我正在制作应用程序首次启动时的教程,并为此使用 UIPageViewController
。一切正常,但 UIPageViewController
的背景颜色没有变透明。一直是黑色,如下图:
下面是我如何添加 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的背景不能设置成透明吗?
请帮忙!
谢谢。
最佳答案
好的,我找到了解决方案:
- 在
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];
- 对于您的 PageViewController
viewControllers
,选择带有图形或任何您想要的 PNG 图像,但要确保背景是透明的。 在
appDelegate.m
中将您的UIPageControl
背景设置为透明UIPageControl *pageControl = [UIPageControl 外观]; pageControl.pageIndicatorTintColor = [UIColor whisperWhite]; pageControl.currentPageIndicatorTintColor = [UIColor whisperDarkGray]; pageControl.backgroundColor = [UIColor clearColor];
现在运行,您的 UIPageViewController
将如下所示(注意背景是静态的,只有文本在移动,因为我们从右向左滑动):
关于ios - UIPageViewController 和 UIPageControl 透明背景色 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33680281/