16

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 3

28

You can use "indicatorStyle" property:

[scrollView1 setBackgroundColor:[UIColor blackColor]];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
3
  • 3
    I 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.
    – Echelon
    Jan 18, 2012 at 20:01
  • 2
    You can set the color of scrollView indicator, for all UIScrollViews in project, with this: [[UIScrollView appearance] setIndicatorStyle:UIScrollViewIndicatorStyleWhite]; Jan 20, 2015 at 11:59
  • 1
    UIScrollView.appearance().indicatorStyle = UIScrollViewIndicatorStyle.White
    – johndpope
    Oct 8, 2015 at 5:59
2

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.

enter image description here

0
//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()
    }
1
  • Note that this may crash if the subview you're referencing is not of the type UIImageView! Nov 13, 2019 at 12:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.