我正在用UIAppearance 为我的 UINavigationBar 设计样式。我希望所有后退按钮都有灰色文本,我的所有 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];
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
];
理论上,灰色文本将应用于所有 UIBarButtonItem。然后我只为 ActionBarButtonItems 用绿色文本覆盖灰色文本。最终结果并不如预期。由于未知原因,每个UIBarButtonItem 都会获得绿色文本。为什么?