2017-04-12 55 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

享受代码。 –