ios - UIBarButtonItem 设计更改第一次不起作用,但第二次不起作用

标签 ios objective-c uistoryboard uibarbuttonitem

我想更改我的 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/

相关文章:

iOS UIWebView loadRequest 耗时太长

ios - EXC_BAD_INSTRUCTION 在 dispatch_get_current_queue()

objective-c - 何时使用 NSEnumerationConcurrent

iphone - Storyboard在 View 之间传递图像

ios - 如何在使用 Storyboards 关闭模态视图 Controller 之前推送 viewController?

ios - 使用 Storyboard展开,如何从我返回的 View Controller 获取数据并在我返回的 View Controller 中使用它?

ios - 如何正确设置我的约束?

ios - iPhone - 振动 5 次?

objective-c - 在 Interface Builder 中使用 UITapGestureRecognizers 的问题

iphone - Xcode 构建正常,但存档失败,找不到 -load_all 的库