我想更改我的 UIBarButtonItem
的设计,它在一定程度上适用于以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [[UIImage imageNamed:@"header.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
[[UIBarButtonItem appearance] setBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
问题是应用程序第一次加载按钮的背景图像不会改变,但如果我离开 View 并返回,背景会更改为我想要的。这里有什么问题?我使用 Storyboard。
最佳答案
要使用 UIAppearance
,您应该在加载 View 或创建控件之前设置控件的外观。如果您以编程方式创建控件,您可以像(示例代码)那样做
[[UIStepper appearance]setTintColor:[UIColor redColor]];
UIStepper *stepper = [[UIStepper alloc]init];
[self.view addSubview: stepper];
或尝试在 application didFinishLaunchingWithOptions
中设置外观。
来自 UIAppearance.h
To customize the appearances for instances of a class contained within an instance of a container class, or instances in a hierarchy, use +appearanceWhenContainedIn: for the appropriate appearance proxy. For example:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:myNavBarColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UIPopoverController class], nil] setTintColor:myPopoverNavBarColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil] setTintColor:myToolbarColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], [UIPopoverController class], nil] setTintColor:myPopoverToolbarColor];
关于ios - UIBarButtonItem 设计更改第一次不起作用,但第二次不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18849769/