2010-07-20 140 views
1

我遇到問題。我使用一個按鈕作爲BarButtonItem。它迄今爲止工作正常,但我的backgroundcolor只有當我點擊我的按鈕時才起作用。我怎樣才能讓我的backgroundcolor每次都會被設置?更改UIButton IPhone的背景顏色

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

爲什麼'blue:5.0'? – kennytm 2010-07-20 07:30:21

+1

5倍於1! :) – willcodejavaforfood 2010-07-20 07:46:06

+0

對不起,是藍色的:1.0 – lightwave 2010-07-20 09:21:46

回答

0

您可以使用此行代碼爲您導航欄

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

的按鈕使用

導航按鈕那裏。

+0

如果我的回答對你有幫助,然後提供給其他人,並勾選這個答案,並對我的信件感到抱歉,你可以自己糾正它。謝謝 – vishiphone 2012-05-03 05:25:09

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

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