Objective c UItabaritem:覆盖子类中的默认外观
我有一个奇怪的问题,让我解释一下:我使用Objective c UItabaritem:覆盖子类中的默认外观,objective-c,nsattributedstring,Objective C,Nsattributedstring,我有一个奇怪的问题,让我解释一下:我使用[uitabaritem外观]来更改我应用程序中所有uitabaritem的字体和颜色。这就像一个符咒,所有的uitabaritems样式都正确 代码是: // Set the normal state [[UITabBarItem appearance] setTitleTextAttributes: @{ UITextAttributeTextColor: AUIColorObject } forState:forState:UIControl
[uitabaritem外观]
来更改我应用程序中所有uitabaritem
的字体和颜色。这就像一个符咒,所有的uitabaritem
s样式都正确
代码是:
// Set the normal state
[[UITabBarItem appearance] setTitleTextAttributes:
@{
UITextAttributeTextColor: AUIColorObject
} forState:forState:UIControlStateHighlighted];
现在我想为单个uitabar
覆盖该样式。我扩展了UITabBar
子类来处理类似样式的东西,在我的例子中,可能的值是RootTabBarControllerStyleDefault
和RootTabBarControllerStyleBox
如何设置此单个uitabaritem
的TitleTextAttributes
?我使用作为子类的UITabBarController
和UITabBar
进行最大限度的控制
更详细地说,这是我的工作方法:
1) uitabar
具有设置外观的静态方法,如下所示:
+ (void)setAppearinaceForStyle:(RootTabBarControllerStyle)_style
{
[[UITabBarItem appearance] setTitleTextAttributes:
@{
UITextAttributeTextColor: (_style == RootTabBarControllerStyleDefault ? UIColor1 : UIColor2)
} forState:UIControlStateHighlighted];
}
2) 然后我为我的UITabBarController
设置了一个代理,执行以下操作:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
[RootTabBar setAppearinaceForStyle:RootTabBarControllerStyleBox];
[tabBarController.tabBar setNeedsDisplay];
for (UITabBarItem *item in tabBarController.tabBar.items)
{
NSString *oldString = item.title;
// Change the title string to force a redraw
[item setTitle:[NSString stringWithFormat:@"%@ ", item.title]];
// Set the title back to its default value
[item setTitle:oldString];
}
[RootTabBar setAppearinaceForStyle:RootTabBarControllerStyleDefault];
}
这允许我为这一个
uitabar
设置不同的样式,但我希望有更好/更干净的方法来实现这一点。UIAppearance文档](
)声明:
使用UIAppearance协议获取类的外观代理。
您可以通过发送
将外观修改消息发送到类的外观代理
注意:当视图进入窗口时,iOS应用外观更改,它
不会更改已在窗口中的视图的外观到
更改当前在窗口中的视图的外观,删除
从视图层次中删除视图,然后将其放回
您应该使用视图层次结构删除包含选项卡栏的视图,然后重新放置它。然而,您的方法是或多或少地做相同的工作(因此有点不优雅)
我认为如果你遵循苹果的doc,结果会是一样的,所以你想定制一个不同的
uitabaritem
如果是这样的话,您已经为单个项目创建了一个子类,只有us
[MyTabBarItemSubclass外观]
而不是用于其他项目的常规[UITabArItem外观]
。谢谢,这似乎是合法的。现在我有一门课有两种“风格”,我想我应该改变这种设计以符合你的模式。