iOS5改变UIToolbar的背景

发布于 2024-12-18 11:58:19 字数 148 浏览 2 评论 0原文

我已阅读“iOS 5 中的新增功能”文档,其中指出,现在可以更好地支持更改某些 UI 元素的背景。

我找不到 iOS 5 更改 UIToolbar 背景图像的正确方法。 iOS 5 是否有一种特殊的新方法可以做到这一点?或者我仍然需要子类化 UIToolbar 吗?

I've read the "What's new in iOS 5" documentation and there it is stated, that changing the background of some UI-Elements is now better supported.

I couldn't find the correct way for iOS 5 to change the background image of an UIToolbar. Is there a special new iOS 5 way of doing this? Or do i still have to subclass the UIToolbar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2024-12-25 11:58:19

是的,有一种新方法可以做到这一点。您可以使用 appearance 使所有 UIToolBar 具有相同的外观。

首先,您必须确保您的类遵循 UIAppearanceContainer 协议。我在我的应用程序委托中完成了它:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UIAppearanceContainer>

@property (strong, nonatomic) UIWindow *window;

@end

然后,您可以在例如 application:didFinishLaunchingWithOptions:viewDidLoad 中设置外观。像这样:

UIImage *image = [UIImage imageNamed:@"myimage.png"];
[[UIToolbar appearance] setBackgroundImage:image forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

您只需执行一次此操作即可为应用中的所有 UIToolBar 获得相同的外观。您还可以设置 UIToolBar 的许多(如果不是全部?)属性。

顺便说一句,有许多类可以遵循 UIAppearanceContainer 协议。要了解使用外观协议可以定制哪些内容,可以打开要定制的类的头文件,如果可以用UIAppearance设置一个属性,那么该属性就有UI_APPEARANCE_SELECTOR 写在属性声明后面。

Yes, there is a new way to do this. You can use appearance to make all UIToolBars have the same appearance.

First, you have to make sure your class follows the UIAppearanceContainer protocol. Here I have done it in my app delegate:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UIAppearanceContainer>

@property (strong, nonatomic) UIWindow *window;

@end

Then you can set the appearance in, for example, application:didFinishLaunchingWithOptions: or viewDidLoad. Like this:

UIImage *image = [UIImage imageNamed:@"myimage.png"];
[[UIToolbar appearance] setBackgroundImage:image forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

You only have to do this once to get the same appearance for all UIToolBars in your app. You can also set many of (if not all?) the properties of your UIToolBar.

As a sidenote, there are many classes that can follow the UIAppearanceContainer protocol. To find out what can be customized using the appearance protocol, you can open the header file of the class you want to customize, if you can set a property with UIAppearance, then the property has UI_APPEARANCE_SELECTOR written behind the property declaration.

木森分化 2024-12-25 11:58:19

这是一个很棒的教程: http://www.raywenderlich.com/ 4344/user-interface-customization-in-ios-5

基本上,它看起来像这样:

UIImage *gradientImage32 = [[UIImage imageNamed:@"surf_gradient_textured_32"] 
    resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[[UINavigationBar appearance] setBackgroundImage:gradientImage32 
    forBarMetrics:UIBarMetricsDefault];

Here's a great tutorial: http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

Basically, it looks like this:

UIImage *gradientImage32 = [[UIImage imageNamed:@"surf_gradient_textured_32"] 
    resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[[UINavigationBar appearance] setBackgroundImage:gradientImage32 
    forBarMetrics:UIBarMetricsDefault];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文