【问题标题】:Create a CAShapeLayer around UIButton?在 UIButton 周围创建一个 CAShapeLayer?
【发布时间】:2015-09-10 07:48:20
【问题描述】:

我有一个 UIButton,我想在其内容之外创建一个 BORDER。

我想使用 CAShapeLayer 或任何其他适当的方式来创建它。

我该怎么做。

UIButtonborderColorBorderWidth属性在BUTTON内部创建一个边框。宽度越大,从内部消耗的空间就越多。

谢谢。

【问题讨论】:

  • 你在外面是什么意思?您无法在按钮框架之外创建或添加任何内容。边框颜色/宽度是在框架内完成的,所以没有办法让它看起来远离框架。

标签: ios objective-c uibutton cashapelayer


【解决方案1】:
// 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

【讨论】:

    猜你喜欢
    • 2018-06-07
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    相关资源
    最近更新 更多