如何设置自定义UINavigationBarButton颜色 [英] How to set custom UINavigationBarButton color

查看:86
本文介绍了如何设置自定义UINavigationBarButton颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的UINavigationBar上的按钮设置一个自定义颜色,但只有当一个特定的UIViewController显示在它(我知道这是可疑的UI设计,但它是一个特定的客户要求)。

I'm trying to set a custom color for a button on my UINavigationBar, but only when a particular UIViewController is being displayed in it (I know this is dubious UI design, but it's a specific customer requirement).

我们当前的实现使用Swizzle覆盖 drawRect 所有 UINavigationBar 对象。这是必要的,因此我们的自定义导航栏颜色应用于标准iOS屏幕(例如通过标准API添加新联系人时)。

Our current implementation uses a Swizzle to override drawRect for all UINavigationBar objects. This is necessary so our custom navigation bar colors are applied to standard iOS screens (like when adding a new contact through standard APIs).

我要更改颜色一个特定的UIViewController的导航栏不同于其他的,但Swizzle总是优先的。

I want to change the tint color of the navigation bar for one particular UIViewController to be different from the others, but the Swizzle always takes precedence.

我在iOS 4.3模拟器测试。我们支持iOS 3.x客户和更高版本(iPhone,iPod和iPad),所以我不能只使用较新的iOS 5 API来自定义它。

I'm testing in the iOS 4.3 Simulator. We support iOS 3.x clients and above (iPhone, iPod and iPad), so I can't just use the newer iOS 5 APIs to customize it.

我试过,没有成功:

  • Adding setStyle calls (as per this workaround) to make the button update again.

我尝试了 UINavigationButton hack我发现这里(我知道这是一个私人API,冒着苹果拒绝应用程序的风险)。我试着将该代码放在 viewWillAppear 中。

I tried the UINavigationButton hack I found here (I know this is a private API and risks Apple rejecting the app). I tried putting that code in viewWillAppear as well.

推荐答案

我通过创建一个具有自定义背景图片的UIButton来工作,如同这个Stackoverflow答案

I got this to work by creating a UIButton with a custom background image, as per this Stackoverflow answer.

如果它能帮助任何人,这里是我的代码:

In case it helps anybody, here's my code:

#import <QuartzCore/QuartzCore.h>

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"mybackground.png"] forState:UIControlStateNormal];
[button setTitle:NSLocalizedString(@"Login", @"Button (9 chars limit) - Login") forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[button.layer setCornerRadius:5.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor: [[UIColor grayColor] CGColor]];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button addTarget:self action:@selector(myLoginActionMethod:) forControlEvents:UIControlEventTouchUpInside];

self._loginButton= [[UIBarButtonItem alloc] initWithCustomView:button]; 

一般来说,我认为最好在UIViewController中设置这样的色调由于 drawRect swizzle在我的代码中的 UINavigationBar 上,因此这不起作用):



In general, I think it is best to set the tint in the UIViewController like this (although in my case this doesn't work due to the drawRect swizzle being in place on the UINavigationBar in my code):

self.navigationController.navigationBar.tintColor=[UIColor blueColor];

这篇关于如何设置自定义UINavigationBarButton颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆