UIPageViewController和UIPageControl透明背景颜色 - iOS

9

当应用程序首次启动时,我正在制作一个教程,并使用UIPageViewController。一切都运行良好,但是UIPageViewController的背景颜色无法透明。它总是黑色的,如下所示:

enter image description here enter image description here

我将向您展示如何添加UIPageViewController(在选项卡控制器中)。

.h文件:

@interface CHMainTabViewController : UITabBarController <UIPageViewControllerDataSource>

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

viewDidLoad中的.m文件:

// 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 的背景颜色上。将控制器的背景设置为透明不可行吗?
请帮忙!
谢谢。
2个回答

4

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

  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看起来会像这样,(注意背景是静态的,只有文本从右向左滑动): enter image description here


2
在呈现页面视图控制器之前,请将这些设置为页面视图控制器。
pageViewController.providesPresentationContextTransitionStyle = YES;
pageViewController.definesPresentationContext = YES;
[pageViewController setModalPresentationStyle:UIModalPresentationOverCurrentContext];

这解决了我背景没有图像的问题,我希望页面控制器后面的内容能够显示出来。 - Jon Hargett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接