2017-04-12 65 views
1

我對iOS中的UIButton有個疑問。 我從庫中使用Apple UIButton,我需要它,當它被按下時變成「被選中」並且它的背景是藍色的。 我做任何事情,它的工作正常,但圍繞標題的「標籤」,我的顏色更加黑暗。 我嘗試將標籤背景設置爲clearColor,但它不是標籤的顏色,我不知道哪個按鈕子視圖是。 有什麼想法? 我使用Objective-C,但如果任何人有關於Swift的想法,我也理解。文字邊框UIButton

預先感謝

enter image description here

+0

你檢查它是否是按鈕的背景顏色或不? –

+0

看起來像你爲普通狀態配置設置了藍色。確保背景顏色僅用於選定狀態配置 –

+0

您是否在代碼中設置顏色?如果是這樣,你不需要。你可以從界面生成器配置正常和高亮顯示狀態的文本和背景色... – DonMag

回答

1

寫這些代碼的

viewDidLoad中()

self.btnDemo.layer.borderWidth = 1.0; 
    self.btnDemo.layer.borderColor = [UIColor grayColor].CGColor; 
    [self.btnDemo setTitle:@"ESTERNO" forState:UIControlStateNormal]; 
    [self.btnDemo setTitle:@"ES" forState:UIControlStateHighlighted]; 
    [self.btnDemo setBackgroundImage:[self imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal]; 
    [self.btnDemo setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:0 green:0 blue:255 alpha:1.0]] forState:UIControlStateHighlighted]; 
    [self.btnDemo setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [self.btnDemo setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; 

這裏btnDemo是的UIButton出口

  • 下面的轉換顏色的方法來形象

    - (UIImage *)imageWithColor:(UIColor *)color { 
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 
        UIGraphicsBeginImageContext(rect.size); 
        CGContextRef context = UIGraphicsGetCurrentContext(); 
    
        CGContextSetFillColorWithColor(context, [color CGColor]); 
        CGContextFillRect(context, rect); 
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
        UIGraphicsEndImageContext(); 
    
        return image; 
    } 
    
+0

[button setBackgroundImage:[self imageWithColor:「myColor」] forState:UIControlStateSelected]; 解決我的問題。 – iNiko84

+0

享受代碼。 –