【问题标题】:UIPageViewController and UIPageControl transparent background color - iOSUIPageViewController 和 UIPageControl 透明背景颜色 - iOS
【发布时间】:2015-11-12 20:00:38
【问题描述】:

当应用程序首次启动时,我正在制作一个教程,并为此使用UIPageViewController。一切正常,但UIPageViewController 的背景颜色没有变得透明。一直是黑色的,如下图:

这是我添加 UIPageViewController 的方法(在标签栏控制器中)

.h 文件:

@interface CHMainTabViewController : UITabBarController <UIPageViewControllerDataSource>

@property (strong, nonatomic) UIPageViewController *pageViewController;
@end

.m 文件(在viewDidLoad):

// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;
self.pageViewController.view.backgroundColor = [UIColor clearColor];
TutorialViewController *startingViewController = [self viewControllerAtIndex:0];

                NSArray *viewControllers = @[startingViewController];

                [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self presentViewController:_pageViewController animated:YES completion:nil];

在我的 AppDelegate.m 中,我还将 UIPageControl 的背景颜色设置为清除:

UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor clearColor];
[pageControl setOpaque:NO];
self.window.backgroundColor = [UIColor clearColor];

我知道问题出在UIPageViewController 的背景颜色上。不能将Controller的背景设置为透明吗?

请帮忙!

谢谢。

【问题讨论】:

    标签: ios objective-c uipageviewcontroller uipagecontrol uibackgroundcolor


    【解决方案1】:

    好的,我找到了解决方案:

    1. UIPageViewController上设置背景图片。

    UIView *view = [[UIView alloc] init]; view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_backgroung"]]; imageView1.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); [view addSubview:imageView1];

    // Create page view controller
    self.pageViewController = [self.storyboard 
    
    instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;
    self.pageViewController.view.backgroundColor = [UIColor clearColor];                  
    [self.pageViewController.view insertSubview:view atIndex:0];
    
    1. 对于您的 PageViewController viewControllers ,选择带有图形或任何您想要的任何内容的 PNG 图像,但请确保背景是透明的。
    2. appDelegate.m中将UIPageControl的背景设置为透明

      UIPageControl *pageControl = [UIPageControl appearance]; pageControl.pageIndicatorTintColor = [UIColor whisperWhite]; pageControl.currentPageIndicatorTintColor = [UIColor whisperDarkGray]; pageControl.backgroundColor = [UIColor clearColor];

    现在运行,您的UIPageViewController 将如下所示(注意背景是静态的,只有文本在移动,因为我们从右向左滑动):

    【讨论】:

      【解决方案2】:

      在呈现之前将这些设置到页面视图控制器

      pageViewController.providesPresentationContextTransitionStyle = YES;
      pageViewController.definesPresentationContext = YES;
      [pageViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];
      

      【讨论】:

      • 这解决了我没有图像作为背景的问题,我希望页面控制器后面的内容能够通过。
      猜你喜欢
      • 2012-11-05
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 2019-09-29
      • 2021-07-02
      相关资源
      最近更新 更多