Apple indirectly prevents the scroll indicators from constantly displaying in the iOS Human Interface Guide , but the recommendations are only recommendations for a reason, they are not considered for each scenario, and sometimes you may need to politely ignore them.
The scroll indicators for any content views are UIImageView subselects of these content views. This means that you can access the scroll indicators of the UIScrollView like any other subspecies of it (ie myScrollView.subviews ) and change the scroll indicators like you would a UIImageView (for example, scrollIndicatorImageView.backgroundColor = [UIColor redColor]; ) .
The most popular solution is the following code:
#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
which is originally credited to this source .
This defines a category for the UIImageView , which defines a custom parameter for the alpha property. This works because at some point in the base code for the UIScrollView it will set its own scroll attribute alpha property attribute to 0 to hide it. At this stage, it will go through our category, and if the UIScrollView hosting has the correct tag, it will ignore the given value, leaving it displayed.
To use this solution, make sure your UIScrollView has an appropriate tag, for example. 
If you want to display the scroll indicator from the moment its UIScrollView visible, simply start the scroll indicators when the .eg view appears
- (void)viewDidAppear:(BOOL)animate { [super viewDidAppear:animate]; [self.scrollView flashScrollIndicators]; }
Additional SO links:
- UIScrollView - shows the scroll bar
- Is the UIScrollView indicator always showing?
- Scroll Ratios Visibility
- Make scrollbars always visible in uiscrollview
Elliott James Perry Mar 25 '13 at 11:31 2013-03-25 11:31
source share