UIPageViewController页面控制背景颜色

时间:2013-08-23 17:10:00

标签: ios objective-c uipageviewcontroller

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

enter image description here

9 个答案:

答案 0 :(得分:84)

您可以使用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];

UIPageControl中只包含MyViewController个对象才能获得这种风格。

修改 屏幕底部UIPageControl周围的黑色背景是由UIPageViewController而非UIPageControl的背景颜色决定的。您可以按以下方式更改此颜色:

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

答案 1 :(得分:25)

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

答案 2 :(得分:8)

只是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;
    }
  }
}

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

答案 3 :(得分:8)

在UIPageViewController中添加以下代码。

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

答案 4 :(得分:3)

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

答案 5 :(得分:3)

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

答案 6 :(得分:2)

以下是已接受答案的 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
}

答案 7 :(得分:0)

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

_

}

答案 8 :(得分:0)

请参阅设置方法的代码,在其中更改页面视图控制器的页面控件颜色,并且对我有用。

self.pop()