ios Changing UIScrollView scrollbar color to different colors

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

提交回复
热议问题