2012-01-30 66 views
9

我的应用程序正在放置一个带有自定义背景图像的按钮,我不需要在它周围有边框。 我不知道如何删除边框。 我的代码是:ios5:删除UIButton边框

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame=CGRectMake(100, 170, 100,30); 


    UIImage *cbutton = [UIImage imageNamed:@"pdficon_small.png"]; 
    [button setImage:cbutton forState:UIControlStateNormal]; 
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button2 addTarget:self 
       action:@selector(openWordings:) 
    forControlEvents:UIControlEventTouchDown]; 


    [button setTag:2]; 
    [self.view addSubview:button]; 

在此先感谢。

回答

14

我假设你的意思是有一个边界button。 要删除它取代线

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

与此

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

你在做什么,现在正在创建一个标准的圆角矩形按钮,然后将图像在上面。 使用UIButtonTypeCustom将创建一个本质上是“空白”按钮,允许您使用您的图像。

2

我正在使用使用默认iOS7按钮的xib文件。除非用户使用iOS6,否则我喜欢它们 - 然后他们会碰到难看的iOS6圆角按钮。这是一个完整的破解,但解决了我的问题。它只是在按钮的每一侧插入4层以掩盖外观。

- (void)maskUglyIos6Border:(UIButton *)button 
{ 
    CALayer *layer = [CALayer layer]; 
    // top layer 
    layer.frame = CGRectMake(0, 0, button.layer.frame.size.width, 5); 
    layer.backgroundColor = [UIColor whiteColor].CGColor; 
    [button.layer addSublayer:layer]; 

    // bottom layer 
    CALayer *layer2 = [CALayer layer]; 
    layer2.frame = CGRectMake(0, button.layer.frame.size.height - 5, button.layer.frame.size.width, 5); 
    layer2.backgroundColor = [UIColor whiteColor].CGColor; 
    [button.layer addSublayer:layer2]; 

    // left layer 
    CALayer *layer3 = [CALayer layer]; 
    layer3.frame = CGRectMake(0, 0, 5, button.layer.frame.size.height); 
    layer3.backgroundColor = [UIColor whiteColor].CGColor; 
    [button.layer addSublayer:layer3]; 

    // right layer 
    CALayer *layer4 = [CALayer layer]; 
    layer4.frame = CGRectMake(button.layer.frame.size.width - 5, 0, 5, button.layer.frame.size.height); 
    layer4.backgroundColor = [UIColor whiteColor].CGColor; 
    [button.layer addSublayer:layer4]; 

} 

为了保持与iOS7的外观,我会建议禁用突出,而是使用showsTouchWhenHighlighted

1

由于我使用普通的白色背景,这为我解决了它。我从UIButton继承,所以我可以使用Storyboard轻松分配它。

@implementation MyButton 

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    // adjust buttons -> "remove" border if we don't run on ios 7 
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { 
     [self setBackgroundColor:[UIColor whiteColor]]; 
    } 

} 
return self; 

}