uibarbuttonitem 隐藏设置了disable灰色为什么不起作用

为什么无法设置 UIBarButtonItem Disabled状态的颜色_问答_ThinkSAAS
为什么无法设置 UIBarButtonItem Disabled状态的颜色
为什么无法设置 UIBarButtonItem Disabled状态的颜色
[right setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateDisabled];
这样设置了 Disabled 时候的颜色
self.navigationItem.rightBarButtonItem.enabled=NO 后
依然是灰色的
javascript[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateDisabled];
参照UIBarItem.h里面的
javascript
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (NSDictionary *)titleTextAttributesForState:(UIControlState)state NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
这两个方法是Appearance protocol的方法。
应该是调用方法的时间点上有问题,这个是我的demo代码片段
javascript- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self setNaviagtionItem];
- (instancetype)init
self = [super init];
if (self) {
[self setNaviagtionItem];
- (id)initWithCoder:(NSCoder *)aDecoder
self = [super initWithCoder:aDecoder];
if (self) {
[self setNaviagtionItem];
- (void)setNaviagtionItem
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"bb"style:UIBarButtonItemStyleDone target:self action:nil];
self.navigationItem.leftBarButtonItem = leftI
NSDictionary* textAttributes = [NSDictionary dictionaryWithObject: [UIColor blackColor]
forKey: NSForegroundColorAttributeName];
[[UIBarButtonItem appearance] setTitleTextAttributes: textAttributes
forState: UIControlStateNormal];
NSDictionary* textAttributes1 = [NSDictionary dictionaryWithObject: [UIColor brownColor]
forKey: NSForegroundColorAttributeName];
[[UIBarButtonItem appearance] setTitleTextAttributes: textAttributes1
forState: UIControlStateDisabled];
NSDictionary* textAttributes2 = [NSDictionary dictionaryWithObject: [UIColor yellowColor]
forKey: NSForegroundColorAttributeName];
[[UIBarButtonItem appearance] setTitleTextAttributes: textAttributes2
forState: UIControlStateSelected];
- (void)viewDidLoad {
[super viewDidLoad];
[self setNaviagtionItem];
self.navigationItem.leftBarButtonItem.enabled = NO;
添加你想要问的问题
PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
让ThinkSAAS更好,把建议拿来。
开发客服微信(1)情景:在iOS8.1中,我们通常会利用如下语句,设置全局的导航条按钮item的主题
UIBarButtonItem *item=[UIBarButtonItem appearance];
NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName]=[UIColor orangeColor];
[item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[item setTitleTextAttributes:textAttrs forState:UIControlStateHighlighted];
NSMutableDictionary *dTextAttrs=[NSMutableDictionary dictionaryWithDictionary:textAttrs];
dTextAttrs[NSForegroundColorAttributeName]=[UIColor grayColor];
[item setTitleTextAttributes:dTextAttrs forState:UIControlStateDisabled];
(2)问题是,我们在上面明明设置了item各种状态下的属性(normal,highlighted和disabled),但是当我们在某一个控制器中添加了一个item时,并且设置为disabled状态时,却发现不起作用。&
-(void)setupNavBar{
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(send)];
self.navigationItem.rightBarButtonItem.enabled=NO;
(3)解决方案&
将上面的设置为disabled的语句放置在viewWillAppear中。
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//如果把下面这一句写在ViewDidLoad中,disabled的item颜色没有效果
self.navigationItem.rightBarButtonItem.enabled=NO;
(4)至于为什么会是这样?说实话,我也不是很清楚,之前以为调用顺序的原因(测试顺序正常),后来觉得是viewDidLoad中enabled未赋值(但测试是0,有赋值)。有明白的还请指教。暂且认为是iOS8.1的一个bug吧。在iOS7.1中测试是正常的。
阅读(...) 评论()

我要回帖

更多关于 uibarbuttonitem间距 的文章

 

随机推荐