ios Changing UIScrollView scrollbar color to different colors

后端 未结 17 2348
我在风中等你
我在风中等你 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<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 Demo available on GitHub - https://github.com/developerinsider/UIScrollViewIndicatorColor

    0 讨论(0)
  • 2020-12-05 07:18

    As for iOS 13 subviews changed so adding simple if, solved this issues.

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 13.0) {
        UIView *verticalIndicator = [scrollView.subviews lastObject];
        verticalIndicator.backgroundColor = [UIColor redColor];
    } else {
        UIImageView *verticalIndicator = [scrollView.subviews lastObject];
        verticalIndicator.backgroundColor = [UIColor redColor];
    }
    }
    
    0 讨论(0)
  • 2020-12-05 07:19

    You can change an image of indicator, but you should do this repeadeatly

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.chageScrollIndicator()
    }
    
    func chageScrollIndicator (){
        if let indicator = self.collection.subviews.last as? UIImageView {
            let edge = UIEdgeInsets(top: 1.25,
                                    left: 0,
                                    bottom: 1.25,
                                    right: 0)
            indicator.image = UIImage(named: "ScrollIndicator")?.withRenderingMode(.alwaysTemplate).resizableImage(withCapInsets: edge)
            indicator.tintColor = UIConfiguration.textColor
        }
    }
    

    You can use this 2 image as template:

    0 讨论(0)
  • 2020-12-05 07:21

    If you wish to add image as well, here is the code for Swift 3

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let verticalIndicator = scrollView.subviews.last as? UIImageView
        verticalIndicator?.image = UIImage(named: "imageName")
    }
    

    This works for UITableView and UICollectionView as well.

    0 讨论(0)
  • 2020-12-05 07:21

    You can use custom UIScrollView scrollBars to implement color in scrollbars. For more details look here

    0 讨论(0)
  • 2020-12-05 07:25

    Here's more safe Swift 3 method:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let verticalIndicator = scrollView.subviews.last as? UIImageView
        verticalIndicator?.backgroundColor = UIColor.green
    }
    
    0 讨论(0)
提交回复
热议问题