※バッドノウハウ。
UICollectionView が使えない iOS 5系以前で重宝している GMGridView だが、スクロールインジケータがセルの裏に潜ってしまうというバグ(?)がある。
ビューの再利用機構を備えたスクロールビューを独自に作った場合にこのような現象を見かける事がある。
手っ取り早い対処法として、スクロールビューのサブビュー配列からスクロールインジケータ(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 だった。)