1

我希望我的 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...


}

请注意,在try nr3中,NSLog 可以工作,但它拒绝更改 UINavigationBar 的背景。那么,有什么想法我会从这里做什么?/ 问候

4

1 回答 1

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
于 2013-10-05T09:08:51.690 回答