UIPageViewController页面控制背景颜色

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

在这里输入图像说明

您可以使用appearance来更改UIPageControl的颜色,否则不可访问。 尝试在您的AppDelegate的didFinishLaunchingWithOptions函数中执行,如下所示。

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

要仅将样式应用于特定的视图控制器,您可以使用appearanceWhenContainedIn代替如下所示:

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

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

编辑:在你的屏幕底部UIPageControl周围的黑色背景是由于你的UIPageViewController而不是UIPageControl的背景颜色。 你可以改变这个颜色如下:

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

更新了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的例子给需要它的人。 把它放在你的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() } } } 

只需子类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; } } } 

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

在UIPageViewController中添加下面的代码。

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

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

以下是接受答案的Swift 3 +版本(带有创build的自定义Color的加号):

 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 } 

覆盖PageViewController中的函数viewDidLayoutSubviews()

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