使滚动条在UIScrollView上始终可见?
我需要在viewDidLoad上始终显示一个滚动条,以便用户可以理解有滚动的内容。 我做了以下几点:
[myscrollView flashScrollIndicators];
但是这个滚动条在viewDidLoad之后才会出现一段时间,并且只有当用户触摸屏幕时才会重新出现。
我需要使滚动条始终可见。 我该怎么做?
苹果公司间接劝阻不断地在iOS人机界面指南中显示滚动指标,但指导原则只是一个原因的指导原则,它们没有考虑到每一种情况,有时你可能需要礼貌地忽视它们。
任何内容视图的滚动指标都是这些内容视图的UIImageView
子视图。 这意味着您可以像访问任何其他子视图(即myScrollView.subviews
)一样访问 UIScrollView
的滚动指示器,并像修改任何UIImageView
(例如scrollIndicatorImageView.backgroundColor = [UIColor redColor];
)一样修改滚动指示器。
最stream行的解决scheme似乎是以下代码:
#define noDisableVerticalScrollTag 836913 #define noDisableHorizontalScrollTag 836914 @implementation UIImageView (ForScrollView) - (void) setAlpha:(float)alpha { if (self.superview.tag == noDisableVerticalScrollTag) { if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleLeftMargin) { if (self.frame.size.width < 10 && self.frame.size.height > self.frame.size.width) { UIScrollView *sc = (UIScrollView*)self.superview; if (sc.frame.size.height < sc.contentSize.height) { return; } } } } if (self.superview.tag == noDisableHorizontalScrollTag) { if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleTopMargin) { if (self.frame.size.height < 10 && self.frame.size.height < self.frame.size.width) { UIScrollView *sc = (UIScrollView*)self.superview; if (sc.frame.size.width < sc.contentSize.width) { return; } } } } [super setAlpha:alpha]; } @end
这是最初记入这个来源 。
这为UIImageView
定义了一个类别,为alpha属性定义了一个自定义设置器。 这是UIScrollView
,因为在UIScrollView
的底层代码的某个时刻,它会将其滚动指示器的alpha属性设置为0以隐藏它。 在这一点上,它将贯穿我们的类别,如果托pipeUIScrollView
有正确的标记,它将忽略设置的值,让它显示。
为了使用这个解决scheme确保你的UIScrollView
有适当的标签,例如
如果您想在显示UIScrollView
的时候显示滚动指示器,只需在视图出现时闪动滚动指示器.eg
- (void)viewDidAppear:(BOOL)animate { [super viewDidAppear:animate]; [self.scrollView flashScrollIndicators]; }
其他SO参考文献:
- UIScrollView – 显示滚动条
- UIScrollView指标总是显示?
- 滚动指标可见性
- 使滚动条始终在uiscrollview中可见
我想提供我的解决scheme。 我不喜欢最stream行的变体类别(在类别中重写方法可能是一些不确定的原因,应该在运行时调用什么方法,因为有两个方法具有相同的select器)。 我使用调合。 而且我也不需要使用标签。
添加这个方法到你的视图控制器,你有滚动视图( self.categoriesTableView
在我的情况)
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // Do swizzling to turn scroll indicator always on // Search correct subview with scroll indicator image across tableView subviews for (UIView * view in self.categoriesTableView.subviews) { if ([view isKindOfClass:[UIImageView class]]) { if (view.alpha == 0 && view.autoresizingMask == UIViewAutoresizingFlexibleLeftMargin) { if (view.frame.size.width < 10 && view.frame.size.height > view.frame.size.width) { if (self.categoriesTableView.frame.size.height < self.categoriesTableView.contentSize.height) { // Swizzle class for found imageView, that should be scroll indicator object_setClass(view, [AlwaysOpaqueImageView class]); break; } } } } } // Ask to flash indicator to turn it on [self.categoriesTableView flashScrollIndicators]; }
添加新课程
@interface AlwaysOpaqueImageView : UIImageView @end @implementation AlwaysOpaqueImageView - (void)setAlpha:(CGFloat)alpha { [super setAlpha:1.0]; } @end
滚动指示器(这种情况下的垂直滚动指示器)将始终在屏幕上。
我不知道这是否会起作用。 但只是给你一个提示。
Scrollview内的滚动条是一个Imageview。 这是UIScrollview的子视图
所以得到UIscrollview的滚动条Imageview。 然后尝试将隐藏的图像属性设置为NO或更改Alpha值
static const int UIScrollViewHorizontalBarIndexOffset = 0; static const int UIScrollViewVerticalBarIndexOffset = 1; -(UIImageView *)scrollbarImageViewWithIndex:(int)indexOffset { int viewsCount = [[yourScrollview subviews] count]; UIImageView *scrollBar = [[yourScrollview subviews] objectAtIndex:viewsCount - indexOffset - 1]; return scrollBar; } -(void) viewDidLoad { //Some Code //Get Scrollbar UIImageView *scrollBar = [self scrollbarImageViewWithIndex: UIScrollViewVerticalBarIndexOffset]; //The try setting hidden property/ alpha value scrollBar.hidden=NO; }
从这里得到参考