ios UIPageViewController 页面控件背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18408269/
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-31 00:51:11  来源:igfitidea点击:

UIPageViewController page control background color

iosobjective-cuipageviewcontroller

提问by user1938695

i'm using UIPageViewController in my app and it's working fine. however, it's page control which has been added automatically has a black background which is hiding the current view controller's bottom material (See picture below). is it possible to call the UIPageViewController's page control and change it's color? i want the page control to be shown over the view controller (example, the Path app's walkthrough) like setting the color instead of black to clear.

我在我的应用程序中使用 UIPageViewController 并且它工作正常。但是,自动添加的页面控件具有黑色背景,隐藏了当前视图控制器的底部材质(见下图)。是否可以调用 UIPageViewController 的页面控件并更改其颜色?我希望页面控件显示在视图控制器上(例如,Path 应用程序的演练),例如设置颜色而不是黑色以清除。

enter image description here

在此处输入图片说明

回答by Yas T.

You can use appearanceto change the color of UIPageControl as otherwise it is not accessible. Try doing it in your AppDelegate's didFinishLaunchingWithOptionsfunction as given below.

您可以使用appearance更改 UIPageControl 的颜色,否则无法访问。尝试在您的 AppDelegatedidFinishLaunchingWithOptions函数中执行此操作,如下所示。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIPageControl *pageControl = [UIPageControl appearance];
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    pageControl.backgroundColor = [UIColor blueColor];

    return YES;
}

To apply style only to a particular view controller, you can use appearanceWhenContainedIninstead as following:

要将样式仅应用于特定的视图控制器,您可以使用appearanceWhenContainedIn以下方法:

UIPageControl *pageControl = [UIPageControl appearanceWhenContainedIn:[MyViewController class], nil];

Only UIPageControlobjects contained in the MyViewControllerare going to get this style.

只有UIPageControl包含在 中的对象MyViewController才会获得这种样式。

EDIT:The black background around UIPageControlat the bottom of your screen is due to the background color of your UIPageViewControllernot UIPageControl. You can change this color as following:

编辑:UIPageControl屏幕底部 周围的黑色背景是由于UIPageViewControllernot的背景颜色UIPageControl。您可以按如下方式更改此颜色:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor]; //Set it to whatever you like
}

回答by jlichti

Updated for Swift 3:

Swift 3更新:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    for view in self.view.subviews {
        if view is UIScrollView {
            view.frame = UIScreen.main.bounds
        } else if view is UIPageControl {
            view.backgroundColor = UIColor.clear
        }
    }
}

Swift 2 example for anyone that needs it. Put this inside your UIPageController subclass.

任何需要它的人都可以使用 Swift 2 示例。把它放在你的 UIPageController 子类中。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    for view in self.view.subviews {
        if view is UIScrollView {
            view.frame = UIScreen.mainScreen().bounds
        } else if view is UIPageControl {
            view.backgroundColor = UIColor.clearColor()
        }
    }
}

回答by Brandon Yang

Add the following code in the UIPageViewController.

在 UIPageViewController 中添加以下代码。

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIPageControl appearance] setPageIndicatorTintColor: [UIColor grayColor]];
    [[UIPageControl appearance] setCurrentPageIndicatorTintColor: [UIColor whiteColor]];
    [[UIPageControl appearance] setBackgroundColor: [UIColor darkGrayColor]];
}

回答by orkenstein

Just subclass UIPageViewController and add this code:

只需继承 UIPageViewController 并添加以下代码:

- (void)viewDidLayoutSubviews {
  [super viewDidLayoutSubviews];
  for (UIView *view in self.view.subviews) {
    if ([view isKindOfClass:[NSClassFromString(@"_UIQueuingScrollView") class]]) {
      CGRect frame = view.frame;
      frame.size.height = view.superview.frame.size.height;
      view.frame = frame;
    }
  }
}

This will extend internal scroll view frame.

这将扩展内部滚动视图框架。

回答by Rafat touqir Rafsun

Here is the Swift 2+ version of Yas-T's Answer

这是Yas-T答案的Swift 2+ 版本

//In AppDelegate
let pageControl = UIPageControl.appearance()
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
pageControl.backgroundColor = UIColor.blueColor()

//Or in your ViewController (Only available on IOS 9.0)
if #available(iOS 9.0, *) {
   let pageControl = UIPageControl.appearanceWhenContainedInInstancesOfClasses([ViewController.self])
   pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
   pageControl.currentPageIndicatorTintColor = UIColor.darkGrayColor()
}

回答by AnhSang

Override the function viewDidLayoutSubviews()in PageViewController:

覆盖viewDidLayoutSubviews()PageViewController 中的函数:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    for view in view.subviews{
        if view is UIScrollView{
            view.frame = UIScreen.main.bounds
        }else if view is UIPageControl{
            view.backgroundColor = UIColor.clear
        }
    }
}

回答by Alex

Here is the Swift 3+version of accepted Answer (with a plus of a custom Color created):

这是接受的答案的Swift 3+版本(创建了自定义颜色的加号):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if #available(iOS 9.0, *) {

        let pageControl = UIPageControl.appearance(whenContainedInInstancesOf: [UIPageViewController.self])
        pageControl.pageIndicatorTintColor = UIColor.init(colorLiteralRed: 43/255.0, green: 170/255.0, blue: 226/255.0, alpha: 0.4)
        pageControl.currentPageIndicatorTintColor = UIColor.init(colorLiteralRed: 43/255.0, green: 170/255.0, blue: 226/255.0, alpha: 1.0)        
    }

    return true
}

回答by ALiEN

Well, I found a working solution. Inside your UIPageViewController, just add this code:

好吧,我找到了一个可行的解决方案。在您的 UIPageViewController 中,只需添加以下代码:

-(void)viewDidLayoutSubviews
{
   [super viewDidLayoutSubviews];

   for (UIView *view in self.view.subviews)
   {
       if ([view isKindOfClass:UIScrollView.class])
       {
           CGRect frame      = view.frame;

           frame.size.height = view.superview.frame.size.height;

           view.frame        = frame;
       }

       if ([view isKindOfClass:UIPageControl.class])
       {
           CGFloat newHeight    = 22;

           CGRect frame         = view.frame;

           frame.origin.y      += frame.size.height - newHeight;
           frame.size.height    = newHeight;

           view.frame           = frame;
           view.backgroundColor = UIColor.darkGrayColor;
           view.alpha           = 0.3;
       }
   }

}

}

回答by Krishan kumar

See my setup method's code in which I change page view controller's page control color and it's worked for me.

查看我的设置方法的代码,其中我更改了页面视图控制器的页面控件颜色,它对我有用。

private func setupPageController() {

    self.pageController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    self.pageController?.dataSource = self
    self.pageController?.delegate = self
    self.pageController?.view.backgroundColor = .clear
    self.pageController?.view.frame = CGRect(x: 0,y: 0,width: self.view.frame.width,height: self.view.frame.height)
    self.addChild(self.pageController!)
    self.view.addSubview(self.pageController!.view)

    for item in self.pageController?.view?.subviews ?? []{
        if item is UIPageControl{
            (item as! UIPageControl).pageIndicatorTintColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
            (item as! UIPageControl).currentPageIndicatorTintColor = #colorLiteral(red: 1, green: 0.7489039302, blue: 0, alpha: 1)
            break
        }
    }
    let initialVC = strBoard.instantiateViewController(withIdentifier: "TutorialOneVC")

    self.pageController?.setViewControllers([initialVC], direction: .forward, animated: true, completion: nil)

    self.pageController?.didMove(toParent: self)
}