Custom UINavigationController UIToolbar Background Image(自定义 UINavigationController UIToolbar 背景图片)
问题描述
我有一个使用 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 背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:自定义 UINavigationController UIToolbar 背景图片
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
