自定义UINavigationController UIToolbar背景图像 [英] Custom UINavigationController UIToolbar Background Image
问题描述
我有一个iPhone应用程序使用 UINavigationController
,并想要自定义背景图像的元素。我能够为 UINavigationController的
UINavigationBar
做到这一点很容易使用Objective-C类别如下:
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的
工具栏,而不是只是做一个子视图工具栏,因为我使用幻灯片 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)?
推荐答案
一个类别。
我不太确定为什么类别适用于 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.
code> 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
p>
And this in the the .m file:
#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中选择您的导航控制器,然后在属性检查器检查显示工具栏框。工具栏应显示在 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背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!