Spail
Spail

Reputation: 703

Customizing UIScrollView's scrollbars

Okay, so the subject talks for itself - I need to change default scrollbar with my custom image. I've been looking for a solution that doesn't require you to write your own ScrollView class or use hacks like creating an UIView with scrollbar image and repositioning it as you scroll.

One solution I liked was to use a simple UIScrollView category and to access scrollbars as UIScrollView's subviews: http://leonov.co/2011/04/uiscrollviews-scrollbars-customization/#comment-7909 For some reason, though, this one doesn't work for me. When I create UIScrollView and get its subviews array only views that I manually add to scrollview are shown there. I cannot access scrollbars iterating through subviews array. For example, this code:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10,10,100,100)];  
scrollView.userInteractionEnabled = YES;    
scrollView.bounces = NO;
scrollView.showsHorizontalScrollIndicator = YES;   
NSLog(@"Subviews count is %d", [[scrollView subviews] count]);

will log "Subviews count is 0". Or, if I add X elements to scrollview, "Subviews count is X". Any ideas?

Upvotes: 0

Views: 1248

Answers (1)

Nick Lockwood
Nick Lockwood

Reputation: 40995

UIScrollbar scroll views are only created when the view is scrolling. They are removed again when the view stops scrolling. That's probably why you can't find them with your category.

You could move your scrollview subview traversing code into the scrollview delegate's scrollViewDidScroll method, which would mean it gets executed whenever the view is scrolling.

I can't help but feel that this is all a horrible and unneccesary hack though, and you'd be better off hiding the scrollbars and implementing them yourself using the delegate methods to determine when to show and hide your custom scrollbar view and the contentOffset property to determine where to position it.

Upvotes: 1

Related Questions