2012-01-30 27 views
9

Mi aplicación está colocando un botón con una imagen de fondo personalizada y no necesito un borde alrededor. No estoy seguro de cómo quitar el borde. Mi código es:ios5: borrar el borde 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]; 

Gracias de antemano.

Respuesta

14

Supongo que quiere decir que hay una frontera alrededor de button. para quitarlo reemplazar la línea

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

con este

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

Lo que está haciendo en este momento es crear un botón estándar RoundedRect, a continuación, colocar una imagen en la parte superior. usando UIButtonTypeCustom creará lo que es esencialmente un botón "en blanco", que le permite usar su imagen.

2

Estoy usando un archivo xib que usa los botones predeterminados de iOS7. Me gustan, a menos que el usuario esté en iOS6, y luego recibe los botones redondeados de iOS6. Este es un truco completo, pero resolvió mis problemas. Simplemente inserta 4 capas en cada lado del botón que enmascara la apariencia.

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

} 

Para mantener con la aparición iOS7, recomendaría deshabilitar destacando y en su lugar usando showsTouchWhenHighlighted.

1

Como utilizo un fondo blanco normal, esto lo resolvió para mí. Yo heredo de UIButton, así que puedo asignarlo fácilmente usando el guión gráfico.

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

}