suchen

Heim  >  Fragen und Antworten  >  Hauptteil

ios - 为什么无法设置 UIBarButtonItem Disabled状态的颜色

[right setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateDisabled];
这样设置了 Disabled 时候的颜色
但是
self.navigationItem.rightBarButtonItem.enabled=NO 后
依然是灰色的

迷茫迷茫2773 Tage vor846

Antworte allen(1)Ich werde antworten

  • 高洛峰

    高洛峰2017-04-17 14:43:08

    应该使用

    javascript[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateDisabled];
    

    参照UIBarItem.h里面的

    javascript/* You may specify the font, text color, and shadow properties for the title in the text attributes dictionary, using the keys found in NSAttributedString.h.
     */
    - (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];
        }
    
        return self;
    }
    
    - (instancetype)init
    {
        self = [super init];
    
        if (self) {
            [self setNaviagtionItem];
        }
    
        return self;
    }
    
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
    
        if (self) {
            [self setNaviagtionItem];
        }
    
        return self;
    }
    
    - (void)setNaviagtionItem
    {
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"bb" style:UIBarButtonItemStyleDone target:self action:nil];
        self.navigationItem.leftBarButtonItem = leftItem;
    
    
    
        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;
    }
    

    Antwort
    0
  • StornierenAntwort