2016-04-24 78 views
0

的頂部我要讓半透明UINavigationBar的(對於iOS9)微紅,我發現下面的代碼是必不可少的:半透明UINavigationBar的不能延伸到屏幕

navController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) 
    navController.navigationBar.shadowImage = UIImage() 
    navController.navigationBar.backgroundColor = UIColor(red: 1.0,green: 0.3,blue: 0.3,alpha: 0.9) 
    navController.navigationBar.translucent = true 

它工作正常,如果我只是創造了一個UINavigationBar的(而不是創建一個UINavigationController),但它看起來像這樣,因爲我創建了一個UINavigationController。

enter image description here 我發現罪魁禍首是線navController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) ,但如果我刪除它,酒吧看起來是這樣的:

enter image description here 所以,我怎麼能解決這個問題? (我創建的UINavigationController編程和抱歉,我不能嵌入圖像的不便)

回答

0

編輯:

您需要繼承UINavigationBar的,爲了得到所需顏色的半透明的背景。看看這個答案:

https://stackoverflow.com/a/19043174/2082569

這裏添加必要的代碼過於供參考:

@interface UnderlayNavigationBar : UINavigationBar 

@end 

@interface UnderlayNavigationBar() 
{ 
    UIView* _underlayView; 
} 

- (UIView*) underlayView; 

@end 

@implementation UnderlayNavigationBar 

- (void) didAddSubview:(UIView *)subview 
{ 
    [super didAddSubview:subview]; 

    if(subview != _underlayView) 
    { 
     UIView* underlayView = self.underlayView; 
     [underlayView removeFromSuperview]; 
     [self insertSubview:underlayView atIndex:1]; 
    } 
} 

- (UIView*) underlayView 
{ 
    if(_underlayView == nil) 
    { 
     const CGFloat statusBarHeight = 20; // Make this dynamic in your own code... 
     const CGSize selfSize = self.frame.size; 

     _underlayView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, selfSize.width, selfSize.height + statusBarHeight)]; 
     [_underlayView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; 
     [_underlayView setBackgroundColor:[UIColor colorWithRed:1.0f green:0.3f blue:0.3f alpha:1.0f]]; 
     [_underlayView setAlpha:0.7f]; 
     [_underlayView setUserInteractionEnabled:NO]; 
    } 

    return _underlayView; 
} 

@end 

UIViewController* rootViewController = ...; 
UINavigationController* navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[UnderlayNavigationBar class] toolbarClass:nil]; 
[navigationController setViewControllers:@[rootViewController]]; 
+0

它仍然不適用於我..它適用於您的設備? – user2232335

+0

對不起,我在我的項目中使用自定義導航欄。從現有的SO上回答了相關部分。編輯答案;請看看,讓我知道它是否有效。當然,答案在Objective-C中,因此您可能需要將其轉換爲swift。 :) – atulkhatri