根据横向/纵向更改UINavigationBar背景?

时间:2013-10-05 08:51:31

标签: ios uinavigationcontroller uinavigationbar landscape portrait

我想让我的UINavigationBar在我的SignatureViewController中正确旋转。目前这是旋转时纵向与横向模式的外观:http://tinypic.com/view.php?pic=2w5573a&s=5#.Uk5kgYapWrg

正如您所看到的,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];
}

尝试nr2:不能正常工作

- (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的背景。那么,任何想法,我将从这里做什么? /问候

1 个答案:

答案 0 :(得分: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