xcode ios5:删除UIButton边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9067721/
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
ios5:delete UIButton border
提问by user973067
My app is placing a button with a custom background image and i doesn't need a border around it. I am not sure how to remove the border. My code is:
我的应用程序正在放置一个带有自定义背景图像的按钮,我不需要它周围的边框。我不确定如何删除边框。我的代码是:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(100, 170, 100,30);
UIImage *cbutton = [UIImage imageNamed:@"pdficon_small.png"];
[button setImage:cbutton forState:UIControlStateNormal];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 addTarget:self
action:@selector(openWordings:)
forControlEvents:UIControlEventTouchDown];
[button setTag:2];
[self.view addSubview:button];
Thanks in advance.
提前致谢。
回答by Amit Shah
I'm assuming you mean there is a border arround button
.
To remove it replace the line
我假设你的意思是有一个边界 arround button
。要删除它,请替换该行
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
with this
有了这个
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
What you are doing right now is creating a standard RoundedRect button, then placing an image over the top.
using UIButtonTypeCustom
will create what is essentially a "blank" button, allowing you to use your image.
您现在正在做的是创建一个标准的 RoundedRect 按钮,然后在顶部放置一个图像。usingUIButtonTypeCustom
将创建本质上是一个“空白”按钮,允许您使用您的图像。
回答by joslinm
I'm using a xib file that uses the default iOS7 buttons. I like them unless the user is on iOS6 -- then they get hit with the ugly iOS6 rounded buttons. This is a complete hack, but solved my problems. It just inserts 4 layers on each side of the button masking the appearance.
我正在使用一个使用默认 iOS7 按钮的 xib 文件。我喜欢它们,除非用户使用 iOS6——然后它们会被丑陋的 iOS6 圆形按钮击中。这是一个完整的黑客,但解决了我的问题。它只是在按钮的每一侧插入 4 层来掩盖外观。
- (void)maskUglyIos6Border:(UIButton *)button
{
CALayer *layer = [CALayer layer];
// top layer
layer.frame = CGRectMake(0, 0, button.layer.frame.size.width, 5);
layer.backgroundColor = [UIColor whiteColor].CGColor;
[button.layer addSublayer:layer];
// bottom layer
CALayer *layer2 = [CALayer layer];
layer2.frame = CGRectMake(0, button.layer.frame.size.height - 5, button.layer.frame.size.width, 5);
layer2.backgroundColor = [UIColor whiteColor].CGColor;
[button.layer addSublayer:layer2];
// left layer
CALayer *layer3 = [CALayer layer];
layer3.frame = CGRectMake(0, 0, 5, button.layer.frame.size.height);
layer3.backgroundColor = [UIColor whiteColor].CGColor;
[button.layer addSublayer:layer3];
// right layer
CALayer *layer4 = [CALayer layer];
layer4.frame = CGRectMake(button.layer.frame.size.width - 5, 0, 5, button.layer.frame.size.height);
layer4.backgroundColor = [UIColor whiteColor].CGColor;
[button.layer addSublayer:layer4];
}
To keep with the iOS7 appearance, I would recommend disabling highlighting and instead using showsTouchWhenHighlighted
.
为了与 iOS7 的外观保持一致,我建议禁用突出显示并使用showsTouchWhenHighlighted
.
回答by HeyMan
Since I use a plain white background this solved it for me. I inherit from UIButton so I can assign it easily using storyboard.
由于我使用纯白色背景,这为我解决了这个问题。我从 UIButton 继承,所以我可以使用故事板轻松分配它。
@implementation MyButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// adjust buttons -> "remove" border if we don't run on ios 7
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
[self setBackgroundColor:[UIColor whiteColor]];
}
}
return self;
}
}