2015-09-10 32 views
0

我有一個UIButton,我想在其內容外創建一個BORDER。圍繞UIButton創建一個CAShapeLayer?

我想使用CAShapeLayer或任何其他適當的方式來創建它。

我該怎麼做。

BORDERCOLOR的UIButton創建按鈕內邊框的屬性BorderWidth。隨着更多寬度,從內部消耗更多空間。

謝謝。

+0

你是什麼意思?你不能創建或添加離開按鈕框。邊框的顏色/寬度是在框架內完成的,所以沒有辦法讓它看起來遠離框架。 –

回答

0
// This is the *.m file 
#import "ViewController.h" 

@interface ViewController() 

// Comment A. Another label and button added to the class without adding them to the 
// storyboard 
@property (nonatomic, strong) UIButton *firstButton; 
@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

self.firstButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[self.firstButton setTitle:@"Just a Button" forState:UIControlStateNormal]; 
self.firstButton.frame = CGRectMake(50, 50, 300, 40); 
UIGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(theAction:)]; 
[self.firstButton addGestureRecognizer:tapGesture]; 

//The screen width and height 
CGRect screenBound = [[UIScreen mainScreen] bounds]; 
CGSize screenSize = screenBound.size; 
CGFloat screenWidth = screenSize.width; 
CGFloat screenHeight = screenSize.height; 

//The CALayer definition 
CALayer *graphic = nil; 
graphic = [CALayer layer]; 
graphic.bounds = CGRectMake(0, 0, screenHeight, screenWidth); 
graphic.position = CGPointMake(screenHeight/2, screenWidth/2); 
graphic.backgroundColor = [UIColor whiteColor].CGColor; 
graphic.opacity = 1.0f; 
[graphic addSublayer:self.firstButton.layer]; 
[self.view.layer addSublayer:graphic]; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

- (IBAction)theAction:(id)sender { 

NSLog(@"Button Tapped"); 
} 
@end