更改 iPhone 的 UIButton 的背景颜色
我有一个问题。我使用 Button 作为 BarButtonItem。到目前为止它工作正常,但我的背景颜色只有在我单击按钮时才有效。我怎样才能让我的背景颜色每次都会被设置?
UIButton *redEmergencyButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
redEmergencyButton.frame = CGRectMake(100, 100, 100, 50);
redEmergencyButton.backgroundColor = [UIColor colorWithRed:0.8
green:0.898039215686274509803
blue:1.0
alpha:1.0];
[redEmergencyButton setTitle:@"Emergency"
forState:UIControlStateNormal];
[redEmergencyButton addTarget:self
action:@selector(doEmergency)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithCustomView:redEmergencyButton];
I have a problem. I use a Button as a BarButtonItem
. It works fine so far, but my backgroundcolor works only if I click on my button. How can I make it so that my backgroundcolor will be set every time ?
UIButton *redEmergencyButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
redEmergencyButton.frame = CGRectMake(100, 100, 100, 50);
redEmergencyButton.backgroundColor = [UIColor colorWithRed:0.8
green:0.898039215686274509803
blue:1.0
alpha:1.0];
[redEmergencyButton setTitle:@"Emergency"
forState:UIControlStateNormal];
[redEmergencyButton addTarget:self
action:@selector(doEmergency)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithCustomView:redEmergencyButton];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用这行代码作为导航栏的
按钮使用
导航按钮。
You can use this line of code for your navigation bar
for button use
navigation button there.
尝试改用此按钮:
Try to use this button instead:
UIButtonTypeRoundedRect 在 iOS7 中已弃用,请使用 UIButtonTypeCustom 代替您的自定义 bgColor。
UIButtonTypeRoundedRect is deprecated in iOS7, Use UIButtonTypeCustom instead for your custom bgColor.