Ios 目标C:创建不带填充的按钮

Ios 目标C:创建不带填充的按钮,ios,objective-c,fill,Ios,Objective C,Fill,如何以编程方式创建圆形按钮,而无需像图像上那样填充 您可以使用一些层属性将大纲添加到任何UIView(UIButton是UIView的子类) 目标-C UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.layer.borderWidth = 2.0; button.layer.borderColor = [UIColor blueColor].CGColor; button.layer.cornerRa

如何以编程方式创建圆形按钮,而无需像图像上那样填充


您可以使用一些
属性将大纲添加到任何
UIView
UIButton
UIView
的子类)

目标-C

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.layer.borderWidth = 2.0;
button.layer.borderColor = [UIColor blueColor].CGColor;
button.layer.cornerRadius = 2.0;
[button setTitle:@"Outline Button" forState:UIControlStateNormal];
Swift

let button = UIButton(type: .System)
button.layer.borderWidth = 2.0
button.layer.borderColor = UIColor.blueColor().CGColor
button.layer.cornerRadius = 2.0
button.setTitle("Outline Button", forState: .Normal)

首先我想说的是,即使他给出了圆角的关键点,上面的答案也没有给出精确的答案

如果你想在没有填充的情况下得到圆角,如上图所示,我们主要应该选择CALayer

您必须首先导入Quartzcore框架。在上面的回答中,他没有提到这一点

 #import <QuartzCore/QuartzCore.h>
UIButton *btnRoundedCorner = [UIButton buttonWithType:UIButtonTypeCustom];
       OR
UIButton *btnRoundedCorner= [UIButton buttonWithType:UIButtonTypeSystem];
btnRoundedCorner.frame = CGRectMake(20, 60, 150, 100);
[btnRoundedCorner setTitle:@"Outline Button" forState:UIControlStateNormal];
[btnRoundedCorner setTitleColor:[UIColor  redColor] forState: UIControlStateNormal];
UIImage *image = [UIImage imageNamed:@"YourImageName"]; //If you want to set the image set here
//If you want to add or set Background image for button
[btnRoundedCorner setBackgroundImage:image forState:UIControlStateNormal];
//If you want to add or set image for button
[btnRoundedCorner setImage:image forState:UIControlStateNormal];
[btnRoundedCorner addTarget:self action:@selector(actionForRoundedCorner:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnRoundedCorner];


//Rounded Corner
btnRoundedCorner.layer.cornerRadius = 2;
btnRoundedCorner.layer.borderWidth = 1;
btnRoundedCorner.layer.borderColor = [UIColor blueColor].CGColor;
btnRoundedCorner.layer.masksToBounds = YES;