更改UIButton IPhone的背景颜色

时间:2010-07-20 07:07:32

标签: ios iphone objective-c uibutton uibarbuttonitem

我有问题。我使用Button作为BarButtonItem。它到目前为止工作正常,但我的backgroundcolor只有在我点击我的按钮时才有效。我该怎么做才能每次都设置我的backgroundcolor?

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

4 个答案:

答案 0 :(得分:0)

setTintColorHow to tint UIBarButtonItem background color?

可能需要UIBarButtonItem

答案 1 :(得分:0)

您可以将这行代码用于导航栏

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

按钮使用

那里有

导航按钮。

答案 2 :(得分:0)

尝试使用此按钮:

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

答案 3 :(得分:0)

在iOS7中不推荐使用UIButtonTypeRoundedRect,而是使用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];