如何使用UIAppearance样式的子类,但不超?(How to use UIAppearance

2019-09-18 02:55发布

我在我的造型与UINavigationBar的UIAppearance 。 我希望所有的后退按钮有灰色的文字,我所有的rightBarButtonItems有绿色文本(颜色是假设)。 由于这两个按键,默认情况下UIBarButtonItems,UIAppearance将无法区分这两个。 所以我决定子类的UIBarButtonItem,称这是ActionBarButtonItem。 我用这个新的子类的任何地方,我需要一个rightBarButtonItem。

rightBarButtonItem

UIBarButtonItem* done = [[ActionBarButtonItem alloc] 
    initWithTitle:@"Done"
    style:UIBarButtonItemStyleDone
    target:self 
    action:@selector(onDonePress)];
self.navigationItem.rightBarButtonItem = done;
[done release];

UIAppearance

NSDictionary* buttonStyle = [NSDictionary 
    dictionaryWithObjects:[NSArray 
        arrayWithObjects:
            [UIColor grayColor],
            , nil
        ]
        forKeys:[NSArray
            arrayWithObjects:
                UITextAttributeTextColor,
                nil
        ]
];

NSDictionary* actionStyle = [NSDictionary 
    dictionaryWithObjects:[NSArray 
        arrayWithObjects:
            [UIColor greenColor],
            nil
        ]
        forKeys:[NSArray
            arrayWithObjects:
                UITextAttributeTextColor,
                nil
        ]
]; 

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil] 
    setTitleTextAttributes:buttonStyle
    forState:UIControlStateNormal
];

[[ActionBarButtonItem appearanceWhenContainedIn:[UINavigationController class], nil]
    setTitleTextAttributes:actionStyle
    forState:UIControlStateNormal
];

从理论上讲,灰色文本将适用于所有UIBarButtonItems。 然后我重写与仅ActionBarButtonItems绿色文本灰色文本。 未如预期的最终结果。 不知什么原因, 每次得到的UIBarButtonItem绿色文本。 为什么?

Answer 1:

你都分分级UIBarButton所以我在这里最初的想法是,当你调用appearanceWhenContainedInActionBarButtonItem结果是呼叫的超类appearanceWhenContainedIn ,因此这就是为什么发生这种情况。 这是不理想,但你可以改变视图上的左,右项的外观没有负载或视图将出现在每个视图。

NSDictionary* buttonStyle = [NSDictionary 
                                 dictionaryWithObjects:[NSArray 
                                                        arrayWithObjects:
                                                        [UIColor blueColor],nil]
                                 forKeys:[NSArray
                                          arrayWithObjects:
                                          UITextAttributeTextColor,
                                          nil
                                          ]
                                 ];

NSDictionary* actionStyle = [NSDictionary 
                             dictionaryWithObjects:[NSArray 
                                                    arrayWithObjects:
                                                    [UIColor greenColor],
                                                    nil
                                                    ]
                             forKeys:[NSArray
                                      arrayWithObjects:
                                      UITextAttributeTextColor,
                                      nil
                                      ]
                             ]; 



[self.navigationItem.leftBarButtonItem setTitleTextAttributes:buttonStyle forState:UIControlStateNormal];
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:actionStyle forState:UIControlStateNormal];

您也可以选择把这个地方像修道院您的应用程序委托,因此您可以用更简单的访问[[UIApplication sharedApplication].delegate setBarButtonColors] 另一种选择是在一个类别UINavigationBar



文章来源: How to use UIAppearance to style a subclass but not the superclass?