如何使用UIAppearance来设置子类的样式,而不是超类? [英] How to use UIAppearance to style a subclass but not the superclass?

查看:129
本文介绍了如何使用UIAppearance来设置子类的样式,而不是超类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 UIAppearance 。我想所有的后退按钮有灰色文本,所有我的rightBarButtonItems有绿色文本(颜色是假设的)。由于默认情况下两个按钮都是UIBarButtonItems,UIAppearance将无法区分两者。所以我决定子类化一个UIBarButtonItem,调用它的ActionBarButtonItem。我在需要一个rightBarButtonItem的地方使用这个新的子类。

I'm styling my UINavigationBar with UIAppearance. I want all the back buttons to have gray text and all my rightBarButtonItems to have green text (colors are hypothetical). Since both buttons are UIBarButtonItems by default, UIAppearance would not be able to differentiate the two. So I decided to subclass a UIBarButtonItem, calling it ActionBarButtonItem. I use this new subclass anywhere I need a rightBarButtonItem.

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



UIAppearance



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获取绿色文本。为什么?

Theoretically, the gray text would be applied for all UIBarButtonItems. Then I override that gray text with green text for ActionBarButtonItems only. The final result is not as expected. For unknown reasons, every UIBarButtonItem gets green text. Why?

>

推荐答案

你是子类 UIBarButton 所以我最初的想法是,当你调用 appearanceWhenContainedIn ActionBarButtonItem 结果是对超类 appearanceWhenContainedIn 的调用,因此这是为什么会发生这种情况。这不是理想的,但您可以更改视图上的左侧和右侧项目的外观加载或视图将出现在每个视图中。

You are sub classing UIBarButton so my initial thought here is that when you call appearanceWhenContainedIn on ActionBarButtonItem the result is a call on the super classes appearanceWhenContainedIn and therefore this is why this is happening. It is not ideal but you could change the appearance of the left and right items on the view did load or view will appear in each view.

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 上的类别。

You may also choose to put this somewhere convent like your app delegate so you can access it more simply with [[UIApplication sharedApplication].delegate setBarButtonColors]. Another alternative could be a category on UINavigationBar.

这篇关于如何使用UIAppearance来设置子类的样式,而不是超类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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