ios Changing UIScrollView scrollbar color to different colors

后端 未结 17 2349
我在风中等你
我在风中等你 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:39

    in IOS 13

    Try this one

    func scrollViewDidScroll(_ scrollView: UIScrollView){
    
    
            if #available(iOS 13, *) {
                (scrollView.subviews[(scrollView.subviews.count - 1)].subviews[0]).backgroundColor = UIColor.themeColor(1.0) //verticalIndicator
                (scrollView.subviews[(scrollView.subviews.count - 2)].subviews[0]).backgroundColor = UIColor.themeColor(1.0) //horizontalIndicator
            } else {
                if let verticalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 1)] as? UIImageView) {
                    verticalIndicator.backgroundColor = UIColor.themeColor(1.0)
                }
    
                if let horizontalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 2)] as? UIImageView) {
                    horizontalIndicator.backgroundColor = UIColor.themeColor(1.0)
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-05 07:41

    Try this it would certainly help you

        for ( UIView *view in scrollBar.subviews ) {
    
           if (view.tag == 0 && [view isKindOfClass:UIImageView.class])
           {
            UIImageView *imageView      = (UIImageView *)view;
            imageView.backgroundColor   = [UIColor yellowColor];
           }
        }
    

    Explanation: UIScrollBar is a collection of subviews. Here scrollBar indicator(vertical/horizontal) is the one of the subviews and it's an UIImageView.So if we set custom color to the UIImageView it effects scrollBar Indicator.

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

    I wrote an article about this not so far ago. Unfortunately color of this bars defined by pre-defined images, so if you are going to change the color of bars some extra work will be required. Take a look to following link, you will definitely find an answer here since I tried to solve the same issue.

    http://leonov.co/2011/04/uiscrollviews-scrollbars-customization/

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

    Here is what I did in Swift 4, similar to previous answers. In my case I'm recoloring the image to be invisible, set correct corner radius and only execute this process once.

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let color = UIColor.red
        guard
            let verticalIndicator = scrollView.subviews.last as? UIImageView,
            verticalIndicator.backgroundColor != color,
            verticalIndicator.image?.renderingMode != .alwaysTemplate
        else { return }
        verticalIndicator.layer.masksToBounds = true
        verticalIndicator.layer.cornerRadius = verticalIndicator.frame.width / 2
        verticalIndicator.backgroundColor = color
        verticalIndicator.image = verticalIndicator.image?.withRenderingMode(.alwaysTemplate)
        verticalIndicator.tintColor = .clear
    }
    
    0 讨论(0)
  • 2020-12-05 07:42

    Swift 2.0 :

    Add UIScrollView Delegate.

    func scrollViewDidScroll(scrollView: UIScrollView){
    let verticalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 1)] as! UIImageView)
    verticalIndicator.backgroundColor = UIColor.greenColor()
    
    let horizontalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 2)] as! UIImageView)
    horizontalIndicator.backgroundColor = UIColor.blueColor()
    }
    
    0 讨论(0)
提交回复
热议问题