1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GMGridView 等でスクロールインジケータがビューに潜ってしまう場合の対処法

Last updated at Posted at 2013-11-26

※バッドノウハウ。

UICollectionView が使えない iOS 5系以前で重宝している GMGridView だが、スクロールインジケータがセルの裏に潜ってしまうというバグ(?)がある。

事例
bad.png

ビューの再利用機構を備えたスクロールビューを独自に作った場合にこのような現象を見かける事がある。

手っ取り早い対処法として、スクロールビューのサブビュー配列からスクロールインジケータ(UIImageView)を見つけ出してそいつに bringSubviewToFront: をしてやるという方法がある。
GMGridView の場合は layoutSubviews を少し改造する。

GMGridView.m
// GMGridView は UIScrollView のサブクラス

@property (weak, nonatomic) UIImageView *vScrollIndicator;
@property (weak, nonatomic) UIImageView *hScrollIndicator;

……

- (void)layoutSubviews 
{
	[super layoutSubviews];
	
	
# if FixGMGridViewsScrollIndicator
	// スクロールインジケータを常に最前面に
	{
		if (!self.vScrollIndicator)
		{
			for (UIView *view in self.subviews)
			{
				// iOS 6以前ではスクロールインジケータの幅が7ptなので、決め打ちしている
				if ([view isKindOfClass:[UIImageView class]] && CGRectGetWidth(view.frame) <= 7)
				{
					self.vScrollIndicator = (UIImageView*)view;
					break;
				}
			}
		}
		
		if (self.vScrollIndicator)
		{
			[self bringSubviewToFront:self.vScrollIndicator];
			self.vScrollIndicator.layer.zPosition = UINT_MAX;
		}
		
		
		if (!self.hScrollIndicator)
		{
			for (UIView *view in self.subviews)
			{
				if ([view isKindOfClass:[UIImageView class]] && CGRectGetHeight(view.frame) <= 7)
				{
					self.hScrollIndicator = (UIImageView*)view;
					break;
				}
			}
		}
		
		if (self.hScrollIndicator)
		{
			[self bringSubviewToFront:self.hScrollIndicator];
			self.hScrollIndicator.layer.zPosition = UINT_MAX;
		}
	}
	// ---
# endif
	
    
    if (_rotationActive) 
    {
        ……
}

サブビューに他の UIImageView が含まれる可能性を想定していない、将来的にスクロールインジケータが UIImageView ではなくなる可能性がある、ライブラリの .m を直接書き換えている等とあまり良くはないが、あくまで対症療法として。

(iOS 7 でもスクロールインジケータは UIImageView だった。)

改造後
good.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?