2012-01-30 1 views
9

La mia app posiziona un pulsante con un'immagine di sfondo personalizzata e non ho bisogno di un bordo attorno ad esso. Non sono sicuro di come rimuovere il bordo. Il mio codice è:ios5: elimina bordo 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]; 

Grazie in anticipo.

risposta

14

Suppongo che vogliate dire che c'è una zona di confine button. Per rimuoverlo sostituire la linea

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

con questo

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

Quello che state facendo in questo momento è la creazione di un pulsante standard RoundedRect, poi mettendo un'immagine sopra le righe. utilizzando UIButtonTypeCustom creerà quello che è essenzialmente un pulsante "vuoto", che consente di utilizzare l'immagine.

2

Sto utilizzando un file xib che utilizza i pulsanti predefiniti di iOS7. Mi piacciono a meno che l'utente non sia su iOS6, quindi vengono colpiti dai brutti pulsanti arrotondati di iOS6. Questo è un trucco completo, ma ho risolto i miei problemi. Inserisce solo 4 strati su ciascun lato del pulsante che maschera l'aspetto.

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

} 

Per mantenere con la comparsa iOS7, consiglio disabilitando l'evidenziazione e utilizzando invece showsTouchWhenHighlighted.

1

Poiché utilizzo uno sfondo bianco semplice, questo lo ha risolto per me. Io eredito da UIButton quindi posso assegnarlo facilmente usando lo 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; 

}