如何更改UINavigationBar的背景? [英] How do I change the background of a UINavigationBar?

查看:97
本文介绍了如何更改UINavigationBar的背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的UINavigationBar的背景更改为 [UIColor colorWithImage:] ,但它无法正常工作。我错过了什么?

I'd like to change the background of my UINavigationBar to a [UIColor colorWithImage:], but it isn't working. What am I missing?

编辑:

一旦我创建了我的子类,我在哪里设置UINavigationController来使用它?

Once I've created my subclass, where do I set the UINavigationController to use it?

推荐答案

你可以使用 tintColor 属性可以更改 UINavigationBar 颜色,但要将图像设置为背景,您必须自己提供 UINavigationBar 子类并覆盖 drawRect:方法,例如:

You can use the tintColor property to change the colour of a UINavigationBar, but to set an image as the background you'll have to provide your own UINavigationBar subclass and override the drawRect: method, for example:

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

如果使用Interface Builder构建UI,则使用自定义导航栏,只需在Interface Builder中选择UINavigationBar元素,打开Inspector,在Identity选项卡中指定类字段中的UINavigationBar子类,如下所示:

If you use Interface Builder to build your UI then to use the custom navigation bar, just select the UINavigationBar element in Interface Builder, open the Inspector and in the Identity tab specify your UINavigationBar subclass in the class field, like so:

这篇关于如何更改UINavigationBar的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆