如何更改 UINavigationBar 的背景?
我想将 UINavigationBar 的背景更改为 [UIColor colorWithImage:]
,但它不起作用。我缺少什么?
编辑:
创建子类后,在哪里设置 UINavigationController 来使用它?
I'd like to change the background of my UINavigationBar to a [UIColor colorWithImage:]
, but it isn't working. What am I missing?
EDIT:
Once I've created my subclass, where do I set the UINavigationController to use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
tintColor
属性更改UINavigationBar
的颜色,但要将图像设置为背景,您必须提供您的自己的UINavigationBar
子类并重写drawRect:
方法,例如:如果您使用 Interface Builder 构建 UI,然后要使用自定义导航栏,只需在Interface Builder,打开 Inspector 并在 Identity 选项卡中的类字段中指定 UINavigationBar 子类,如下所示:
You can use the
tintColor
property to change the colour of aUINavigationBar
, but to set an image as the background you'll have to provide your ownUINavigationBar
subclass and override thedrawRect:
method, for example: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+CustomBackground.m
(它向 UINavigationBar 添加自定义类别):To have an image in the navigation bar, you have to draw it yourself, which actually isn't that hard. Save this as
UINavigationBar+CustomBackground.m
(it adds a custom category to UINavigationBar):