UIPageViewController 和 UIPageControl 透明背景色 - iOS

UIPageViewController and UIPageControl transparent background color - iOS

我正在制作应用程序首次启动时的教程,并为此使用 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的背景不能设置成透明吗?

请帮忙!

谢谢。

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

  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 将如下所示(注意背景是静态的,只有文本在移动,因为我们从右向左滑动):

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

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