【问题标题】:How to set background color for UIPageViewController?如何为 UIPageViewController 设置背景颜色?
【发布时间】:2015-10-26 04:33:19
【问题描述】:

如何在UIPageViewController 中设置背景视图的颜色?是否可以在 Storyboard 中执行此操作?

【问题讨论】:

    标签: ios storyboard uipageviewcontroller xcode7-beta4


    【解决方案1】:

    编辑:我认为问题是关于 UIPageControl。

    您应该能够简单地修改 UIPageViewController 的视图颜色:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor blueColor]; //Set to any color.
    }
    

    【讨论】:

      【解决方案2】:

      在 UIPageViewController 的视图中,您可以使用以下代码:

      override func viewDidLoad() {
          super.viewDidLoad()
      
          // Set backbround color to white
          self.view.backgroundColor = UIColor.white
      }
      

      我已将颜色设置为白色,但您几乎可以插入任何您想要的颜色。

      干杯!

      【讨论】:

        【解决方案3】:

        在这种情况下,您需要更改 UIPageViewCotroller 的 UIPageControl。

        试试这个。

        UIPageControl* proxy = [UIPageControl appearanceWhenContainedIn:[self.pageViewController class], nil];
        [proxy setPageIndicatorTintColor:[UIColor lightGrayColor]];
        [proxy setCurrentPageIndicatorTintColor:[UIColor blackColor]];
        [proxy setBackgroundColor:[UIColor whiteColor]];
        

        如果你的项目是 swift 写的,试试这个。

        let proxy: UIPageControl = UIPageControl.appearanceWhenContainedInInstancesOfClasses(self.pageViewController.self)
        proxy.pageIndicatorTintColor = UIColor.lightGrayColor()
        proxy.currentPageIndicatorTintColor = UIColor.blackColor()
        proxy.backgroundColor = UIColor.whiteColor()
        

        【讨论】:

        • 问题是关于 UIPageViewController 的 scrollView 颜色,而不是关于 UIPageControl。
        • 问题是,“如何设置 UIPageViewController 的背景颜色”而不是关于 UIPageControl,请编辑您的答案。
        【解决方案4】:

        是的,您可以更改背景颜色,但它似乎不会在 Interface Builder 中公开。

        按照禁止在 IB 中设置页面视图控制器的方法,您需要:
        1. 将 UIContainerView 添加到您的视图控制器
        2. 将 UIPageViewController 添加到情节提要
        3. 添加一个从 UIViewContainer 到 UIPageViewController 的 Embed segue(标识符为 myPageViewController)

        从您的原始视图控制器中,将其添加到您的 prepareForSegue 方法中:

            if ([segueName isEqualToString: @"myPageViewController"]) {
                /* _pageViewController is a property where I keep a reference to the embedded controller */
                if (_pageViewController == nil) {
                    /* only do this once */
                    _pageViewController = (WePageViewController*)[segue destinationViewController];
                    _pageViewController.dataSource = ...;
                    _pageViewController.delegate = ...;
                    _pageViewController.view.backgroundColor = [UIColor purpleColor];
            }
        

        附言抱歉,我还没有迁移到 Swift...

        【讨论】:

          【解决方案5】:

          要更改背景颜色,请使用UIPageControl 并将backgroundColor 属性分配给所需的UIColor

          UIPageControl *pageControl = [UIPageControl appearance]; pageControl.backgroundColor = [UIColor orangeColor];

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-12-11
            • 2020-12-14
            • 2019-02-28
            • 2013-04-26
            • 1970-01-01
            • 2010-11-08
            • 2020-12-06
            • 1970-01-01
            相关资源
            最近更新 更多