Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS动态更改UINavigationBar';背景与图像_Iphone_Ios_Uinavigationbar - Fatal编程技术网

Iphone iOS动态更改UINavigationBar';背景与图像

Iphone iOS动态更改UINavigationBar';背景与图像,iphone,ios,uinavigationbar,Iphone,Ios,Uinavigationbar,我已经阅读并更改了UINavigationBar的背景: @implementation UINavigationBar (CustomImage) - (void)drawRect:(CGRect)rect { UIImage *image = [UIImage imageNamed: @"top-with-logo.png"]; [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.

我已经阅读并更改了UINavigationBar的背景:

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"top-with-logo.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
你看,我添加了一个名为top-with-logo.png的图像,我想通过添加一个名为top-with-logo.png的图像来更改背景视图。但是上面的代码将永远改变背景,我不能再改变了。你知道怎么做吗


多谢各位

我不确定我是否理解您的意思,但您可以在应用程序的委托上放置一个布尔ivar。然后在函数中,如果为真/假,则加载top-with-logo.png,如果为其他值,则加载top-with-logo.png。然后,当您想要更改徽标时,只需更改布尔值并在导航栏上调用setNeedsDisplay


如果您使用xib文件,还可以将UINavigationBar子类化,而不是使用类别覆盖drawRect函数并将变量作为实例变量作为子类,我相信它更干净,但您只能在Interface builder中设置它(否则,无法告诉UINavigationController为navigationBar属性使用其他类)

我不确定我是否理解你的意思,但你可以在应用程序的代理上放置一个布尔值ivar。然后在函数中,如果是真/假,加载top-with-logo.png,如果是另一个值,则加载top-with-logo.png。然后,当你想更改徽标时,只需更改布尔值并在导航栏上调用setNeedsDisplay。

如果您使用xib文件,还可以将UINavigationBar子类化,而不是使用类别覆盖drawRect函数并将变量作为实例变量作为子类,我相信它更干净,但您只能在Interface builder中设置它(否则无法告诉UINavigationController为navigationBar属性使用其他类)

您可以使用

UINavigationBar *navigationBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed: @"NavBar-Wood.png"];
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];
你可以用

UINavigationBar *navigationBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed: @"NavBar-Wood.png"];
[navigationBar setBackgroundImage:image forBarMetrics: UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor brownColor];

只想添加一点,您实际上可以使用Objc运行时中定义的object_setClass(id object,class class)在代码中设置导航栏的类。只想添加一点,您实际上可以使用Objc运行时中定义的object_setClass(id object,class class)在代码中设置导航栏的类。