简体   繁体   English

如何在UIPageViewController中更改页面指示器的背景颜色

[英]How to change background color of page indicator in UIPageViewController

I use a UIPageViewController in storyboard, and write a class inherit from UIPageViewController. 我在情节提要中使用UIPageViewController,并编写一个从UIPageViewController继承的类。

After I finished code, I found the background color of page indicator is black, which I don't know how to change. 完成代码后,我发现页面指示器的背景颜色是黑色,我不知道该如何更改。

在此处输入图片说明

I search the internet, and found many people will use UIViewController+UIPageControl to do custom page indicator. 我在互联网上搜索,发现很多人会使用UIViewController + UIPageControl来做自定义页面指示器。

Is it possible to change the background color in UIPageViewController? 是否可以在UIPageViewController中更改背景颜色?

Thanks! 谢谢!

by adding: 通过增加:

let pageControl = UIPageControl.init(frame: CGRectMake(0, UIScreen.mainScreen().bounds.size.height*14/15, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
pageControl.backgroundColor = UIColor.init(red: 0, green: 147/255, blue: 229/255, alpha: 1)
view.addSubview(pageControl)

I've changed the color successfully. 我已经成功更改了颜色。

But I've another question. 但是我还有另一个问题。

Although I've set the imageView to fill the full screen, the page control will cover the button of the image, how could I avoid page control cover the image, and only show the dots? 尽管已将imageView设置为全屏显示,但页面控件将覆盖图像的按钮,如何避免页面控件覆盖图像,而仅显示点?

I struggled with this for over two hours. 我为此奋斗了两个多小时。 Here's what I did in Swift 4 这是我在Swift 4中所做的

    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    for view in self.view.subviews{
        if view is UIPageControl{
            (view as! UIPageControl).currentPageIndicatorTintColor = .yellow
            }
        }
    }

Once you have the UIPageControl in this block, you should be able to customize its indicator colours 一旦在该块中具有UIPageControl,就应该能够自定义其指示器颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM