I have a UIScrollView which I am applying a black background, which blends in the scrollbar. I have been looking for a way to change the scrollbars color to white, but I cannot figure it out. Is there a proper way of doing this?
3 Answers
You can use "indicatorStyle" property:
[scrollView1 setBackgroundColor:[UIColor blackColor]];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
-
3I can never remember this property name. Everything else is called scrollIndicatorSomething except this one. Even autocomplete cannot save me from such a badly-named property.– EchelonJan 18, 2012 at 20:01
-
2You can set the color of scrollView indicator, for all UIScrollViews in project, with this: [[UIScrollView appearance] setIndicatorStyle:UIScrollViewIndicatorStyleWhite]; Jan 20, 2015 at 11:59
-
1UIScrollView.appearance().indicatorStyle = UIScrollViewIndicatorStyle.White Oct 8, 2015 at 5:59
Both UIScrollView indicator are sub view of UIScrollView. So, we can access subview of UIScrollView and change the property of subview.
1 .Add UIScrollViewDelegate
@interface ViewController : UIViewController<UIScrollViewDelegate>
@end
2. Add scrollViewDidScroll in implementation section
-(void)scrollViewDidScroll:(UIScrollView *)scrollView1
{
//get refrence of vertical indicator
UIImageView *verticalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-1)]);
//set color to vertical indicator
[verticalIndicator setBackgroundColor:[UIColor redColor]];
//get refrence of horizontal indicator
UIImageView *horizontalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-2)]);
//set color to horizontal indicator
[horizontalIndicator setBackgroundColor:[UIColor blueColor]];
}
Note:- Because these indicator update every time when you scroll (means reset to default). SO, we put this code in scrollViewDidScroll delegate method.
//ScrollView Deleagate
func scrollViewDidScroll(scrollView: UIScrollView){
let verticalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 1)] as! UIImageView)
verticalIndicator.backgroundColor = UIColor.redColor()
let horizontalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 2)] as! UIImageView)
horizontalIndicator.backgroundColor = UIColor.blueColor()
}
-
Note that this may crash if the subview you're referencing is not of the type UIImageView! Nov 13, 2019 at 12:50