46

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

在此处输入图像描述

4

9 回答 9

91

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

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

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

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

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

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor]; //Set it to whatever you like
}
于 2013-08-23T17:25:20.917 回答
28

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()
        }
    }
}
于 2016-02-05T18:11:48.890 回答
10

在 UIPageViewController 中添加以下代码。

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIPageControl appearance] setPageIndicatorTintColor: [UIColor grayColor]];
    [[UIPageControl appearance] setCurrentPageIndicatorTintColor: [UIColor whiteColor]];
    [[UIPageControl appearance] setBackgroundColor: [UIColor darkGrayColor]];
}
于 2015-03-12T03:28:06.027 回答
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;
    }
  }
}

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

于 2014-09-05T08:25:51.227 回答
5

这是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()
}
于 2016-03-30T23:29:20.623 回答
3

这是已接受答案的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
}
于 2017-03-23T22:06:45.160 回答
3

重写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
        }
    }
}
于 2017-10-17T07:36:51.020 回答
0

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

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)
}
于 2020-03-18T18:12:01.707 回答
0

好吧,我找到了一个可行的解决方案。在您的 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;
       }
   }

}

于 2019-01-04T09:38:15.160 回答