繁体   English   中英

UIBarButtonItem设计更改第一次无效,但第二次无效

[英]UIBarButtonItem design change doesn't work the first time but the second

我想更改我的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];
}

问题在于应用程序第一次加载按钮的背景图像不会改变,但是如果我离开视图并返回,背景就会变成我想要的。 这是什么问题? 我使用情节提要。

若要使用UIAppearance ,应在加载视图或创建控件之前设置控件的外观。 如果您以编程方式创建控件,则可以执行以下操作(示例代码)

 [[UIStepper appearance]setTintColor:[UIColor redColor]];
 UIStepper *stepper = [[UIStepper alloc]init];
 [self.view addSubview: stepper];

或尝试在application didFinishLaunchingWithOptions设置外观。

UIAppearance.h

要为包含在容器类的实例中的类的实例或层次结构中的实例自定义外观,请对适当的外观代理使用+ appearanceWhenContainedIn:。 例如:

[[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];

在初始化课程之前,请在您的课程之外调用代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM