ios Changing UIScrollView scrollbar color to diffe

2019-01-17 20:33发布

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.

Many Thanks

13条回答
Emotional °昔
2楼-- · 2019-01-17 21:17

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.

查看更多
唯我独甜
3楼-- · 2019-01-17 21:17

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
}
查看更多
萌系小妹纸
4楼-- · 2019-01-17 21:18

I ran into the same problem recently so I decided to write a category for it.

https://github.com/stefanceriu/UIScrollView-ScrollerAdditions

[someScrollView setVerticalScrollerTintColor:someColor]; 
[someScrollView setHorizontalScrollerTintColor:someColor];`

It blends it with the original image so only the color will change. On the other hand, it can also be modified to provide a custom image for the scrollers to use.

查看更多
萌系小妹纸
5楼-- · 2019-01-17 21:19

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

查看更多
一夜七次
6楼-- · 2019-01-17 21:22

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/

查看更多
聊天终结者
7楼-- · 2019-01-17 21:25

Here's more safe Swift 3 method:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let verticalIndicator = scrollView.subviews.last as? UIImageView
    verticalIndicator?.backgroundColor = UIColor.green
}
查看更多
登录 后发表回答