objective-c 如何为自定义类型 UIButton 设置矩形边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/854306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set rectangle border for custom type UIButton
提问by afin
I am trying to set an image as the background of a custom UIButton. I was able to set a background image for the "rounded rect" UIButton in interface builder, but now the borders have vanished. Is there any way to retain the borders? Also, I would like to make the borders show up rectangular instead of rounded.
我正在尝试将图像设置为自定义 UIButton 的背景。我能够在界面构建器中为“圆形矩形”UIButton 设置背景图像,但现在边框已经消失。有没有办法保留边界?另外,我想让边框显示为矩形而不是圆形。
采纳答案by Kendall Helmstetter Gelner
When you set a background image in a button, the button outlines go away. Include them in your image, if you need different sizes look at the UIImage
当您在按钮中设置背景图像时,按钮轮廓会消失。将它们包含在您的图像中,如果您需要不同的尺寸,请查看 UIImage
stretchableImageWithLeftCapWidth:topCapHeight:
stretchableImageWithLeftCapWidth:topCapHeight:
method.
方法。
回答by trillions
#import <QuartzCore/QuartzCore.h> // don't forget to add this in your file
CALayer * layer = [yourUIButton layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:0.0]; //when radius is 0, the border is a rectangle
[layer setBorderWidth:1.0];
[layer setBorderColor:[[UIColor grayColor] CGColor]];
i know this is an old question, but still i would like to post my answer here. since i was looking for answer but got this resolved myself in an easy way.
我知道这是一个老问题,但我仍然想在这里发布我的答案。因为我正在寻找答案,但我自己以一种简单的方式解决了这个问题。
回答by Jonathan Arbogast
Check out the cornerRadius, borderWidthand borderColorproperties of CALayer. These properties are new as of iPhone OS 3.0.
退房的cornerRadius,borderWidth和borderColor性质CALayer。这些属性是 iPhone OS 3.0 的新属性。
You probably want to subclass UIButtonand then set these properties in the constructor. Your button has a layer property you can access to set these border properties.
您可能想要子类化UIButton,然后在构造函数中设置这些属性。您的按钮有一个图层属性,您可以访问以设置这些边框属性。
Here's an example:
下面是一个例子:
- (id)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
[self.layer setBorderWidth:1.0];
[self.layer setCornerRadius:5.0];
[self.layer setBorderColor:[[UIColor colorWithWhite:0.3 alpha:0.7] CGColor]];
}
return self;
}
回答by Praveen R
You could also "User Defined Runtime Attributes" in IB to set rounded corners like done in image below.
您还可以在 IB 中“用户定义的运行时属性”来设置圆角,如下图所示。
回答by TylerJames
I'd just like to add to the answers provided by nanshi and Jonathan.
我只想补充nanshi和Jonathan提供的答案。
In order to access the borderand cornerRadiusproperties of the layer you will first need to import the QuartzCore framework.
为了访问层的border和cornerRadius属性,您首先需要导入 QuartzCore 框架。
#import <QuartzCore/QuartzCore.h>
#import <QuartzCore/QuartzCore.h>
Otherwise you will not be able to access the properties.
否则您将无法访问这些属性。
回答by Shekhar Gupta
You can set the border properties on the CALayer by accessing the layer property of the button.
您可以通过访问按钮的图层属性来设置 CALayer 上的边框属性。
First, add Quartz
首先,添加石英
#import <QuartzCore/QuartzCore.h>
Set properties:
设置属性:
[[myButton layer] setBorderWidth:2.0f];
[[myButton layer] setBorderColor:[UIColor greenColor].CGColor];

