根据方向改变导航栏的背景图像

时间:2013-07-30 19:39:38

标签: objective-c

有没有办法根据方向更改导航栏的背景图像?我尝试在app委托中使用方法,例如:

-(void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame

{

 UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if(orientation == (UIInterfaceOrientationMaskPortrait)||orientation==(UIInterfaceOrientationMaskPortraitUpsideDown)){

    UIImage *myImage = [UIImage imageNamed:@"borderP.png"];
    UIImage * imageBottom = [UIImage imageNamed:@"bottomP.png"];
    [[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];
    [[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    NSLog(@"******P******");
}

else if(orientation == (UIInterfaceOrientationMaskLandscapeLeft||orientation== UIInterfaceOrientationMaskLandscapeRight)){


    UIImage *myImage = [UIImage imageNamed:@"borderL.png"];
    UIImage * imageBottom = [UIImage imageNamed:@"borderL.png"];
    [[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:myImage forBarMetrics:UIBarMetricsDefault];
    [[UIToolbar appearance] setBackgroundImage:imageBottom forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    NSLog(@"******L********");
}

}  

但背景图像不会改变。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

外观代理仅适用于新实例。要修改已存在的实例,您需要直接在其上调用setBackgroundImage:forToolbarPosition:barMetrics: