ios - UIPageViewController 和 UIPageControl 透明背景色 - iOS

标签 ios objective-c uipageviewcontroller uipagecontrol uibackgroundcolor

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

enter image description here enter image description here

下面是我如何添加 UIPageViewController(在标签栏 Controller 中)

.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 外观]; pageControl.pageIndicatorTintColor = [UIColor whisperWhite]; pageControl.currentPageIndicatorTintColor = [UIColor whisperDarkGray]; pageControl.backgroundColor = [UIColor clearColor];

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

关于ios - UIPageViewController 和 UIPageControl 透明背景色 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33680281/

相关文章:

swift - 如何检测第一个应用程序启动并显示说明屏幕?

objective-c - 将 Obj C 代码转换为 Swift weak self

iphone - Xcode : How would I do a one-off UIAlertView?

objective-c - 类型化的 Objective-C 集合到 Swift

iphone - 触摸位置高亮

ios - 为什么我的 tableview 很慢?

ios - Core Data一对多逆向关系

objective-c - UIPageViewController - 如何跳转到特定页码?

ios - 在 iOS 7 上将 Realm 与 Swift 结合使用

ios - 带有自动布局的 UIScrollView 失败,并在启动时消失