1

我想我的UINavigationBar在我的SignatureViewController中正確旋轉。這是目前它在旋轉時看起來如何在人像和風景模式中的示例:http://tinypic.com/view.php?pic=2w5573a&s=5#.Uk5kgYapWrg根據風景/人像更改UINavigationBar背景?

正如你所看到的,UINavigationBar在橫向上不會縮放寬度。我的項目是建立如下:

RootViewController的UINavgationController)------ 模態推 ----->SignatureViewController

(portrait only)         (portrait/landscape) 

請注意,我不是使用SignatureViewController的正常推送,我使用的是模態。以防萬一我想提到這一點,因爲我不確定它是否會以任何奇怪的方式影響結果?......我嘗試了不同的方法來改變NavigationBar的背景,具體取決於它是橫向還是縱向。我會張貼一些這裏如下:

嘗試NR1:不工作

- (void) viewWillAppear:(BOOL)animated { 

[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault]; 
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"navbar25g.png"] forBarMetrics:UIBarMetricsLandscapePhone]; 

[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 
} 

嘗試NR 2:不工作

- (void) viewWillAppear:(BOOL)animated { 

UIImage *navBarLandscape = [[UIImage imageNamed:@"navbar25g.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(x,x,x,x)]; 

[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault]; 
[[UINavigationBar appearance]setBackgroundImage:navBarLandscape forBarMetrics:UIBarMetricsDefault]; 

[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 

} 

嘗試NR3:不工作

- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification 
{ 

UIView *v = [[UIView alloc]init]; 

int a = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue]; 
int w = [[UIScreen mainScreen] bounds].size.width; 
int h = [[UIScreen mainScreen] bounds].size.height; 
switch(a){ 
    case 4: 
     v.frame = CGRectMake(0,20,w,h); 
     [[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone... 
     NSLog(@"willrotate:1"); 

     break; 
    case 3: 
     v.frame = CGRectMake(-20,0,w-20,h+20); 
     [[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"navbar25g.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone... 
     NSLog(@"willrotate:2"); 
     [self.view addSubview:v]; 
     break; 
    case 2: 
     v.frame = CGRectMake(0,-20,w,h); 
     [[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone... 
     NSLog(@"willrotate:3"); 

     break; 
    case 1: 
     v.frame = CGRectMake(20,0,w-20,h+20); 
     NSLog(@"willrotate:4"); 
     [[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"navbar25g.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone... 


} 

請注意,在嘗試nr3 NSLog工作,但它拒絕更改UINavigationBar的背景。那麼,我想從這裏做什麼想法? /問候

回答

0

能不能請你來實現UINavigationBar類並覆蓋一個drawRect:方法

它應該工作

@implementation UINavigationBar (BackgroundImage) 

- (void)drawRect:(CGRect)rect 
{ 
    UIImage *navimage; 
    //we will check if screen width is 320 then it will be potrait 
    if(self.frame.size.width == 320) 
    { 
     navimage = [UIImage imageNamed: @"Your Potrait nav bar image"]; 
    } 
    else 
    { 
     navimage = [UIImage imageNamed: @"Your Landscape nav bar image"]; 
    } 

    [navimage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 

@end 

希望它可以幫助...

,只是嘗試一下本作NR1和nr2

只是改變這個代碼的最後一行...如果它的工作或沒有...讓我知道

self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; //UIViewAutoresizingFlexibleWidth 
+0

對不起,延遲迴復,但我沒有工作:( – user1293618