2014-05-23 85 views
1

如何在具有圓角的UIButton中添加右側和左側邊框?在帶圓角的UIButton中添加底部邊框

這是我的按鈕,我想圓角邊框太:

我的按鈕的源代碼:

- (void)initialise; 
    { 
     //add border 
     [self addBottomBorderWithColor:[UIColor grayColor] andWidth:2]; 
     [self addRightBorderWithColor:[UIColor grayColor] andWidth:2]; 

     //round 
     self.layer.cornerRadius = 8.0f; 
    } 

    - (void)addTopBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth 
    { 
     CALayer *border = [CALayer layer]; 
     border.backgroundColor = color.CGColor; 

     border.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth); 
     [self.layer addSublayer:border]; 
    } 

    - (void)addRightBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth 
    { 
     CALayer *border = [CALayer layer]; 
     border.backgroundColor = color.CGColor; 

     border.frame = CGRectMake(self.frame.size.width, 0, borderWidth, self.frame.size.height); 
     [self.layer addSublayer:border]; 
    } 

回答

3

,你可以添加一個CALayer的稍大比你的按鈕和抵消它與負邊緣,使頂部和左邊界被隱藏。記住,我現在不能測試它,它可能需要進一步調整,但它應該給你的總體思路:

-(void)initialise; 
{ 
    //add border 
    [self addBorderWithColor:[UIColor grayColor] andWidth:2]; 
    //round 
    self.layer.cornerRadius = 8.0f; 
} 

- (void)addBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth 
{ 
    CALayer *border = [CALayer layer]; 
    border.borderColor = color.CGColor; 
    border.frame = CGRectMake(-borderWidth, -borderWidth, self.frame.size.width+borderWidth, self.frame.size.height+borderWidth); 
    border.borderWidth = borderWidth; 
    border.cornerRadius = 8.f; 
    [self.layer addSublayer:border]; 
    self.layer.masksToBounds = YES; 
} 
+1

怎麼樣自動佈局?在旋轉設備的情況下,您需要更改CALayer幀。 – TomSawyer