如何使UIPageViewController背景半透明?

时间:2014-03-26 04:10:53

标签: ios uipageviewcontroller

我在网上搜索并找到类似的答案(How do I make the bottom bar with dots of a UIPageViewController translucent?),但它并没有解决我的问题。

这是我的代码

    // PageControl Layout
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor clearColor];
//    pageControl.frame = CGRectMake(5,5,305,400);
pageControl = [UIPageControl appearanceWhenContainedIn:[self class], nil];

indexCurrentPage = 0;
pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
[pageController.view setFrame:[[UIScreen mainScreen] bounds]];
NSLog(@"page controller.fram  is %@", [NSValue valueWithCGRect:[[pageController view] frame]]);
pageController.dataSource = self;

GuidedTourPage *initialViewController = [self viewControllerAtIndex:0];
[[self.pageController view] setFrame:[[initialViewController view] bounds]];
NSLog(@"bound is %@", [NSValue valueWithCGRect:[[initialViewController view] bounds]]);
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[self.view addSubview:[pageController view]];
[pageController didMoveToParentViewController:self];

滚动功能和其他一切正常。除了有一部分图像丢失。 我在我的" AppDelegate.m"中做了self.window.backgroundColor = [UIColor whiteColor];。所以pageControl.backgroundColor = [UIColor clearColor];会使背景为白色,这是可以理解的。

但我想要这样的东西(http://www.appcoda.com/wp-content/uploads/2013/06/UIPageViewController-Tutorial-Screen.jpg),其中pageControl具有透明背景。

我知道问题在于图层。但我不知道如何解决它。 有没有办法让NSLog层层次结构检查呢?或者是否有任何测试人员软件可以帮助我测试图层问题。 我不认为这将是一个非常难的问题,只要我能找到一种调试图层的方法,比如Firefox中的3D-Inspect元素功能。

提前致谢

3 个答案:

答案 0 :(得分:4)

试试这个:

UIPageControl *pageControl = [UIPageControl appearance];
pageControl.backgroundColor = [UIColor clearColor];

你也可以玩Alpha:

[[UIColor alloc] initWithRed:0.0f green:0.0f blue:0.0f alpha:0.7];

所以,声明将成为:

pageControl.backgroundColor = [[UIColor alloc] initWithRed:0.0f green:0.0f blue:0.0f alpha:0.7];

答案 1 :(得分:1)

这样的事应该可以正常工作:

[self.pageCont setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.5]];

答案 2 :(得分:1)

我已验证此解决方案, 请将UIPageViewController子类化并在实现中添加此方法。

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[NSClassFromString(@"_UIQueuingScrollView") class]]) {
            CGRect frame = view.frame;
            frame.size.height = view.superview.frame.size.height;
            view.frame = frame;
            [self.view sendSubviewToBack:view];
        }
    }
}

这将解决问题。 感谢

相关问题