如何使用 UIAppearance 为子类设置样式而不是超类?

2024-02-11

我正在设计我的 UINavigationBar 样式UI外观 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html。我希望所有后退按钮都有gray文本和所有我要拥有的 rightBarButtonItemsgreen文本(颜色是假设的)。由于默认情况下这两个按钮都是 UIBarButtonItems,因此 UIAppearance 无法区分这两个按钮。所以我决定对 UIBarButtonItem 进行子类化,将其命名为 ActionBarButtonItem。我在需要 rightBarButtonItem 的任何地方都使用这个新子类。

右栏按钮项

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

UI外观

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 的绿色文本覆盖该灰色文本。最终的结果并不如预期。由于未知的原因,everyUIBarButtonItem 获取绿色文本。为什么?


您正在分类UIBarButton所以我最初的想法是当你打电话时appearanceWhenContainedIn on ActionBarButtonItem结果是对超级类的调用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.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 UIAppearance 为子类设置样式而不是超类? 的相关文章

随机推荐