Make scrollbar always visible in UIScrollView?

I need the scrollbar to always be visible in viewDidLoad so that the user can understand that there is scrollable content. I have done the following:

[myscrollView flashScrollIndicators]; 

But then the scroll bars only appear for a while after viewDidLoad and disappear again to reappear when the user touches the screen.

I need to make scrollbars always visible. How can i do this?

+14
ios iphone cocoa-touch uikit uiscrollview
Dec 04 '12 at 6:23
source share
3 answers

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. Tag

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
+15
Mar 25 '13 at 11:31
source share
β€” -

I want to offer my solution. I don’t like the most popular option with a category (overriding methods in a category can cause some uncertainty about which method should be called at runtime, since there are two methods with the same selector). Instead, I use swizzling. And also I do not need to use tags.

Add this method to your view controller where you have the scroll ( self.categoriesTableView in my case)

 - (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]; } 

Add new class

 @interface AlwaysOpaqueImageView : UIImageView @end @implementation AlwaysOpaqueImageView - (void)setAlpha:(CGFloat)alpha { [super setAlpha:1.0]; } @end 

The scroll indicator (vertical scroll indicator) will always be displayed in this window.

+2
Nov 30 '16 at 13:52
source share

I don't know if this will work or not. But just a clue for you.

The scrollbar inside Scrollview is an image. What is the subtask of UIScrollview

So, get the scroll image in the user interface view. Then try setting the hidden image property to NO or changing the Alpha value

 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; } 

Get the link from here.

0
Dec 04
source share



All Articles