2014-03-26 60 views
2

我在網上搜索,發現類似這樣的答案(How do I make the bottom bar with dots of a UIPageViewController translucent?),但它不能解決我的問題。如何讓UIPageViewController背景半透明?

這裏是我的代碼

// 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元素函數這樣的圖層。

在此先感謝

回答

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

東西像這樣的壽LD做工精細:

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

這樣的工作,但問題是,你不能使它完全透明,因爲pageControl上方的視圖不會延伸超過page'Control的結束。 I.E.您必須將內容視圖展開到pageControl的背景中。 – TheJeff

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]; 
     } 
    } 
} 

這將解決問題。 謝謝

+0

我遇到的唯一問題是,當我點擊頁面滾動到下一個頁面時,我的視圖會以大約底部導航欄事物的高度「跳轉」子視圖元素的位置。我試過再次對子視圖進行佈局,但它似乎不能很好地工作。 – TheJeff