问题描述
第一次启动应用程序并使用 UIPageViewController
时,我正在制作教程。一切正常,但 UIPageViewController
的背景颜色没有透明。它总是黑色的,如下所示:
I'm making a tutorial when app launches for the first time and using UIPageViewController
for that. Everything is working fine but the background color of UIPageViewController
is not getting transparent. It's always black, as shown below:
以下是我如何添加 UIPageViewController
(在标签栏控制器中)
Here's how I'm adding the UIPageViewController
(in a tabbar 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
要清除的背景颜色:
in my AppDelegate.m, I've also set UIPageControl
's background color to clear:
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的背景设置为透明?
I know that the issue is with the UIPageViewController
's background color. Is it not possible to set a Controller's background to be transparent?
请帮忙!
谢谢。
推荐答案
好的我找到了解决方案:
Okay I've found the solution:
- 在
UIPageViewController
上设置背景图片。
- Set a background image on the
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);
[查看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图像,但要确保背景是透明的。 -
设置你的
UIPageControl
的背景在中是透明的appDelegate.m
- For your PageViewController
viewControllers
, choose a PNG Image with graphics or whatever you want, but make sure the background is transparent. Set your
UIPageControl
's background as transparent inappDelegate.m
UIPageControl * pageControl = [UIPageControl外观];
pageControl.pageIndicatorTintColor = [UIColor whisperWhite];
pageControl.currentPageIndicatorTintColor = [UIColor whisperDarkGray];
pageControl.backgroundColor = [UIColor clearColor];
现在运行并且你的 UIPageViewController
将如下所示(注意背景静态,只有文字正在移动,因为我们从右向左滑动):
Now run and your UIPageViewController
will look like this, (notice the background static and only the text is moving, as we swipe from right to left):
这篇关于UIPageViewController和UIPageControl透明背景色 - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!