【问题标题】:Target only bar buttons of the top navigation bar using the appearance proxy使用外观代理仅定位顶部导航栏的栏按钮
【发布时间】:2013-02-25 07:12:50
【问题描述】:

我正在使用外观代理来设置我的应用程序的样式,但是 [UIBarButtonItem 外观] 有问题;我只想为顶部 UINavigationbar 的按钮设置样式,但是当我运行下面的代码时,相同的样式也会应用于键盘的完成按钮。

NSDictionary *btnAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                              [UIColor blackColor],
                              UITextAttributeTextColor,
                              [UIColor clearColor],
                              UITextAttributeTextShadowColor, nil];

[[UIBarButtonItem appearance] setTitleTextAttributes: btnAttributes
                                            forState: UIControlStateNormal];

所以,我的问题是:是否有可能 - 使用外观代理 - 只为顶部导航栏的栏按钮设置样式?

【问题讨论】:

    标签: ios objective-c uibarbuttonitem


    【解决方案1】:

    您可以将外观限制为某些容器类。来自appearance reference

    appearanceWhenContainedIn:
    

    返回给定容器中接收器的外观代理 等级制度。 (必填)

    + (id)appearanceWhenContainedIn:(Class <UIAppearanceContainer>)ContainerClass,...
    

    参数 容器类, 以零结尾的外观容器类列表。 返回值 给定包含层次结构中接收器的外观代理。

    讨论 对于 var-args 列表中不是符合 UIAppearanceContainer 协议的 Class 对象的任何项,此方法都会引发异常。

    可用性 适用于 iOS 5.0 及更高版本。


    对于你的例子,那将是:

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

    【讨论】:

    • 在使用像MPMoviePlayerController 这样的类时要小心,因为该野兽实际上使用了UINavigationController 的子类,但不符合外观要求。不管怎样,很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多