更改 iPhone 的 UIButton 的背景颜色

发布于 2024-09-10 17:31:39 字数 910 浏览 7 评论 0原文

我有一个问题。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单 2024-09-17 17:31:39

您可以使用这行代码作为导航栏的

[self.navigationcontroller.navigationbar setTintColor:[UIColor bluecolor]];

按钮使用

导航按钮。

You can use this line of code for your navigation bar

[self.navigationcontroller.navigationbar setTintColor:[UIColor bluecolor]];

for button use

navigation button there.

尘曦 2024-09-17 17:31:39

尝试改用此按钮:

UIBarButtonItem* emergencyButton = [[UIBarButtonItem alloc]initWithTitle:@"Emergency" style:UIBarButtonItemStyleBordered target:self action:@selector(doEmergency)];
[emergencyButton setTintColor: [UIColor colorWithRed:0.8 
                                                  green:0.898039215686274509803
                                                   blue:1.0 
                                                  alpha:1.0];

Try to use this button instead:

UIBarButtonItem* emergencyButton = [[UIBarButtonItem alloc]initWithTitle:@"Emergency" style:UIBarButtonItemStyleBordered target:self action:@selector(doEmergency)];
[emergencyButton setTintColor: [UIColor colorWithRed:0.8 
                                                  green:0.898039215686274509803
                                                   blue:1.0 
                                                  alpha:1.0];
呆头 2024-09-17 17:31:39

UIButtonTypeRoundedRect 在 iOS7 中已弃用,请使用 UIButtonTypeCustom 代替您的自定义 bgColor。

UIButton *redEmergencyButton = [[UIButton buttonWithType:UIButtonTypeCustom] 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];

UIButtonTypeRoundedRect is deprecated in iOS7, Use UIButtonTypeCustom instead for your custom bgColor.

UIButton *redEmergencyButton = [[UIButton buttonWithType:UIButtonTypeCustom] 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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文