2012-01-30 4 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입니다. 은 당신이 지금하고있는 다음 맨 위에 이미지를 배치, 표준 RoundedRect 버튼을 만드는 무엇이

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

으로 라인을

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

을 대체 제거하십시오. UIButtonTypeCustom을 사용하면 본질적으로 "빈"버튼이 만들어 지므로 이미지를 사용할 수 있습니다.

2

기본 iOS7 버튼을 사용하는 xib 파일을 사용하고 있습니다. 사용자가 iOS6을 사용하지 않는 한 나는 그 (것)들을 좋아한다 - 그 때 추악한 iOS6 둥근 단추에 명중을 얻는다. 이것은 완전한 해킹이지만 내 문제를 해결했습니다. 모양을 마스킹하는 버튼의 각면에 4 개의 레이어를 삽입하기 만하면됩니다. iOS7에 모양으로 유지하기 위해

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

} 

, 나는 강조 비활성화하고 대신 showsTouchWhenHighlighted를 사용하는 것이 좋습니다 것입니다.

1

나는 평범한 흰색 배경을 사용하기 때문에 이것을 해결했습니다. 스토리 보드를 사용하여 쉽게 할당 할 수 있도록 UIButton에서 상속받습니다.

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

}