ios Changing UIScrollView scrollbar color to different colors

后端 未结 17 2394
我在风中等你
我在风中等你 2020-12-05 07:16

How can we change color of UIScrollview\'s scroll indicator to something like blue, green etc.

I know we can change it to white, black. But other then these colors.

17条回答
  •  借酒劲吻你
    2020-12-05 07:18

    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
    @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 Demo available on GitHub - https://github.com/developerinsider/UIScrollViewIndicatorColor

提交回复
热议问题