ios 将 UIScrollView 滚动条颜色更改为不同的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12005187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:46:03  来源:igfitidea点击:

ios Changing UIScrollView scrollbar color to different colors

iosuiscrollview

提问by Mann

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

我们如何将 UIScrollview 的滚动指示器的颜色更改为蓝色、绿色等。

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

我知道我们可以把它改成白色、黑色。但除了这些颜色。

Many Thanks

非常感谢

回答by Kaan Dedeoglu

Unfortunately you can't, of course you can always roll your own. These are your options:

不幸的是你不能,当然你总是可以推出自己的。这些是您的选择:

UIScrollViewIndicatorStyleDefault:

The default style of scroll indicator, which is black with a white border. This style is good against any content background.

UIScrollViewIndicatorStyleBlack:

A style of indicator which is black and smaller than the default style. This style is good against a white content background.

UIScrollViewIndicatorStyleWhite:

A style of indicator is white and smaller than the default style. This style is good against a black content background.

UIScrollViewIndicatorStyleDefault:

滚动指示器的默认样式,黑色带白色边框。这种风格适用于任何内容背景。

UIScrollViewIndicatorStyleBlack:

一种黑色且小于默认样式的指示器样式。这种风格很适合白色内容背景。

UIScrollViewIndicatorStyleWhite:

指示器的样式为白色且小于默认样式。这种风格适合黑色内容背景。

回答by Andrey Gordeev

Here's more safe Swift 3method:

这是更安全的Swift 3方法:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let verticalIndicator = scrollView.subviews.last as? UIImageView
    verticalIndicator?.backgroundColor = UIColor.green
}

回答by Vineet Choudhary

Both UIScrollView indicator are sub view of UIScrollView. So, we can access subview of UIScrollView and change the property of subview.

两个 UIScrollView 指示器都是 UIScrollView 的子视图。因此,我们可以访问 UIScrollView 的子视图并更改子视图的属性。

1 .Add UIScrollViewDelegate

1 .添加 UIScrollViewDelegate

@interface ViewController : UIViewController<UIScrollViewDelegate>
@end

2. Add scrollViewDidScroll in implementation section

2.在实现部分添加scrollViewDidScroll

-(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.

注意:- 因为每次滚动时这些指标都会更新(意味着重置为默认值)。所以,我们把这段代码放在 scrollViewDidScroll 委托方法中。

enter image description hereDemo available on GitHub - https://github.com/ioshacks/UIScrollViewIndicatorColor

在此处输入图片说明GitHub 上提供的演示 - https://github.com/ioshacks/UIScrollViewIndicatorColor

回答by A.G

Swift 2.0 :

斯威夫特 2.0:

Add UIScrollView Delegate.

添加 UIScrollView 委托。

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()
}

回答by akaDuality

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: Use this image for @2xUse this image for @3x

您可以使用这 2 个图像作为模板: 将此图像用于@2x将此图像用于@3x

回答by Jaydip

in IOS 13

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)
            }
        }
    }

回答by Alex

in IOS 13

IOS 13

Since iOS13 scroll indicators have class _UIScrollViewScrollIndicator, not UIImageView.

由于 iOS13 滚动指示器具有类 _UIScrollViewScrollIndicator,而不是 UIImageView。

Many people used code like

很多人使用过这样的代码

let verticalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 1)] as! UIImageView)

It's not good idea, because they promised that last subview will be UIImageView :). Now it's not and they can get crash.

这不是个好主意,因为他们承诺最后一个子视图将是 UIImageView :)。现在不是,他们可能会崩溃。

You can try following code to get scrollView indicators:

您可以尝试以下代码来获取 scrollView 指标:

extension UIScrollView {

    var scrollIndicators: (horizontal: UIView?, vertical: UIView?) {

        guard self.subviews.count >= 2 else {
            return (horizontal: nil, vertical: nil)
        }

        func viewCanBeScrollIndicator(view: UIView) -> Bool {
            let viewClassName = NSStringFromClass(type(of: view))
            if viewClassName == "_UIScrollViewScrollIndicator" || viewClassName == "UIImageView" {
                return true
            }
            return false
        }

        let horizontalScrollViewIndicatorPosition = self.subviews.count - 2
        let verticalScrollViewIndicatorPosition = self.subviews.count - 1

        var horizontalScrollIndicator: UIView?
        var verticalScrollIndicator: UIView?

        let viewForHorizontalScrollViewIndicator = self.subviews[horizontalScrollViewIndicatorPosition]
        if viewCanBeScrollIndicator(view: viewForHorizontalScrollViewIndicator) {
            horizontalScrollIndicator = viewForHorizontalScrollViewIndicator
        }

        let viewForVerticalScrollViewIndicator = self.subviews[verticalScrollViewIndicatorPosition]
        if viewCanBeScrollIndicator(view: viewForVerticalScrollViewIndicator) {
            verticalScrollIndicator = viewForVerticalScrollViewIndicator
        }
        return (horizontal: horizontalScrollIndicator, vertical: verticalScrollIndicator)
    }

}

If you need only one (h or v indicator) - it's better to cut this func and keep only one you need (to improve perfomance).

如果您只需要一个(h 或 v 指示器) - 最好削减此功能并只保留一个您需要的功能(以提高性能)。

Also it would be good to call update func inside of DispatchQueue, to keep smoothness of scrolling.

此外,最好在 DispatchQueue 内部调用 update func,以保持滚动的平滑度。

func scrollViewDidScroll(_ scrollView: UIScrollView) {
   DispatchQueue.main.async {
       scrollView.updateCustomScrollIndicatorView()
   }
}

回答by j_gonfer

Based on the answer of @Alex (https://stackoverflow.com/a/58415249/3876285), I'm posting just a little improvement to change the color of scroll indicators.

根据@Alex ( https://stackoverflow.com/a/58415249/3876285)的回答,我发布了一些改进来更改滚动指示器的颜色。

extension UIScrollView {

    var scrollIndicators: (horizontal: UIView?, vertical: UIView?) {

        guard self.subviews.count >= 2 else {
            return (horizontal: nil, vertical: nil)
        }

        func viewCanBeScrollIndicator(view: UIView) -> Bool {
            let viewClassName = NSStringFromClass(type(of: view))
            if viewClassName == "_UIScrollViewScrollIndicator" || viewClassName == "UIImageView" {
                return true
            }
            return false
        }

        let horizontalScrollViewIndicatorPosition = self.subviews.count - 2
        let verticalScrollViewIndicatorPosition = self.subviews.count - 1

        var horizontalScrollIndicator: UIView?
        var verticalScrollIndicator: UIView?

        let viewForHorizontalScrollViewIndicator = self.subviews[horizontalScrollViewIndicatorPosition]
        if viewCanBeScrollIndicator(view: viewForHorizontalScrollViewIndicator) {
            horizontalScrollIndicator = viewForHorizontalScrollViewIndicator.subviews[0]
        }

        let viewForVerticalScrollViewIndicator = self.subviews[verticalScrollViewIndicatorPosition]
        if viewCanBeScrollIndicator(view: viewForVerticalScrollViewIndicator) {
            verticalScrollIndicator = viewForVerticalScrollViewIndicator.subviews[0]
        }
        return (horizontal: horizontalScrollIndicator, vertical: verticalScrollIndicator)
    }

}

If you don't add .subviews[0], you will get the deeper view and when you try to change the color of the indicator, this will appear with a weird white effect. That's because there is another view in front of it:

如果您不添加.subviews[0],您将获得更深入的视图,并且当您尝试更改指示器的颜色时,这将出现奇怪的白色效果。那是因为它前面还有另一个视图:

enter image description here

在此处输入图片说明

By adding .subviews[0]to each indicator view, once you try to change the color by calling:

通过添加.subviews[0]到每个指标视图,一旦您尝试通过调用更改颜色:

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    DispatchQueue.main.async() {
        scrollView.scrollIndicators.vertical?.backgroundColor = UIColor.yourcolor
    }
}

You will access to the first view and change the color properly:

您将访问第一个视图并正确更改颜色:

enter image description here

在此处输入图片说明

Kudos to @Alex who posted a great solution

感谢@Alex 发布了一个很好的解决方案

回答by karthik

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.

说明: UIScrollBar 是子视图的集合。这里的滚动条指示器(垂直/水平)是子视图之一,它是一个 UIImageView。所以如果我们为 UIImageView 设置自定义颜色,它会影响滚动条指示器。

回答by Purushottam Padhya

This is how the color of the scroll bar is changed:

这是滚动条颜色的改变方式:

    //scroll view  
    UIScrollView *scView = [[UIScrollView alloc] init];  
    scView.frame = self.view.bounds; //scroll view occupies full parent views  
    scView.contentSize = CGSizeMake(400, 800);  
    scView.backgroundColor = [UIColor lightGrayColor];  
    scView.indicatorStyle = UIScrollViewIndicatorStyleBlack;  
    scView.showsHorizontalScrollIndicator = NO;  
    scView.showsVerticalScrollIndicator = YES;  
    scView.scrollEnabled = YES;  
    [self.view addSubview: scView];