我有一个使用 UINavigationController
的 iPhone 应用程序,并希望使用自定义背景图像自定义元素.我可以很容易地使用如下的 Objective-C 类别为 UINavigationController's
UINavigationBar
做到这一点:
I have an iPhone application using UINavigationController
and would like to customize the elements with custom background images. I was able to do this for the UINavigationController's
UINavigationBar
pretty easily using Objective-C categories as below:
http://foobarpig.com/iphone/uinavigationbar-with-solid-color-or-image-background.html
我想对 UINavigationController 的
UIToolbar
做同样的事情,但同样的方法似乎不起作用(尽管我完全不知道为什么不这样做.) 我环顾四周,人们似乎建议子类化 UIToolbar
,但这对于 UINavigationController 的
工具栏是不可能的,它是一个只读 UIToolbar代码>.我想使用
UINavigationController's
工具栏而不是仅仅制作子视图工具栏,因为我使用的是滑入式 setToolbarHidden
动画.
I'd like to do the same for the UINavigationController's
UIToolbar
, but the same approach doesn't seem to work (although I have absolutely no idea why not.) I've looked around and people seem to suggest subclassing UIToolbar
, but this isn't possible for the UINavigationController's
toolbar, which is a read-only UIToolbar
. I want to use the UINavigationController's
toolbar instead of just making a subview toolbar because I'm using the slide-in setToolbarHidden
animation.
任何人都知道是否可以将背景图像应用到这个 UINavigationController
工具栏(很可能通过某种方式覆盖 drawRect
方法)?
Anyone have any idea if it's possible to apply a background image to this UINavigationController
toolbar (most likely by somehow overriding the drawRect
method)?
你需要子类而不是创建一个类别.
You need to subclass instead of creating a category.
我不太清楚为什么一个类别适用于 UINavigationBar
而不是 UIToolbar
,但子类化适用于它们,我认为这是更标准的自定义方式类似的东西.
I'm not too sure why a category works for UINavigationBar
but not UIToolbar
, but subclassing works for both of them and I think it's the more standard way of customizing stuff like that.
所以要继承 UIToolbar
,创建一个名为 MyToolbar
的类(或类似的东西)并将其放在 .h 中:
So to subclass UIToolbar
, create a class called MyToolbar
(or something similar) and put this in the .h:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface MyToolbar : UIToolbar {}
- (void)drawRect:(CGRect)rect;
@end
这在 .m 文件中:
#import "MyToolbar.h"
@implementation MyToolbar
- (void)drawRect:(CGRect)rect {
backgroundImage = [UIImage imageNamed:@"my-awesome-toolbar-image.png"];
[backgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
从那里你必须告诉导航控制器使用 MyToolbar
而不是 UIToolbar
.我发现最简单的方法是在 Interface Builder 中选择您的 Navigation Controller,然后在 Attributes Inspector 中选中Shows Toolbar"框.工具栏应显示在 NavigationController
窗口中.单击它,然后在 Identity Inspector 中将类更改为 MyToolbar
.
From there you've got to tell the Navigation Controller to use MyToolbar
instead of UIToolbar
. Easiest way I've found to do that is to select your Navigation Controller in Interface Builder, then in the Attributes Inspector check the 'Shows Toolbar' box. The toolbar should show up in the NavigationController
window. Click on it and then in the Identity Inspector change the class to MyToolbar
.
这篇关于自定义 UINavigationController UIToolbar 背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!