在呈现模态视图控制器时更改UINavigationBar背景的问题

时间:2012-03-09 20:43:26

标签: objective-c ios uinavigationbar

我正在使用this方法更改uiviewcontrollers的背景。它通常在我推动视图控制器时起作用。

但是,如果使用

显示viewcontroller
[self presentModalViewController:customViewController animated:YES];

然后,这段代码不起作用。任何人都可以建议什么是错的?

使用的代码:

要在导航栏中显示图像,您必须自己绘制图像,这实际上并不难。将其保存为UINavigationBar + CustomBackground.m(它将自定义类别添加到UINavigationBar):

@implementation UINavigationBar (CustomBackground)

- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"NavMain.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

4 个答案:

答案 0 :(得分:2)

如果您在iOS 5上运行,则不再调用drawRect:

您需要使用UIAppearance或子类UINavigationController并使用它来更改为您的图像。

可以找到UIAppearance的教程here

drawRect:仍适用于iOS 5以下的版本)

答案 1 :(得分:0)

不确定是不是这样,但你试过吗

[self presentViewController:customViewController animated:YES completion:NULL]

设置你认为适合customViewController的modalViewStyle?根据{{​​3}},不推荐使用presentModalViewController,因此使用上面的消息调用可能会更好运(特别是因为你似乎只是遇到模态视图控制器的这个问题)

答案 2 :(得分:0)

您必须创建一个导航控制器,设置根控制器并显示它。

e.g。

UIViewController * vc = [[UIViewController alloc] init];

UINavigationController * cntrol = [[UINavigationController alloc] initWithRootViewController:vc];

[self presentModalViewController:cntrol animated:YES];

答案 3 :(得分:0)

您在导航控制器中设置导航栏图像的方法。

但是您提供的新视图控制器不使用导航方法。相反,您使用模态视图控制器。

我有两个解决方案:

--- 1 -----

仍然使用模态视图控制器,只需将该图像添加到新视图控制器的顶部。

[self.view addSubview:theImage];

---- 2 -----

使用

[self.navigationController pushViewController:customViewController animated:YES];

而不是

[self presentModalViewController:customViewController animated:YES];   

在这种情况下,您正在使用导航。

我会建议第二个。