iOS 按钮 - 添加边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18295703/
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
iOS Buttons - add border
提问by user1509593
I am making my app ready for iOS7. I did conversion and was working with a user. The button in the app does not look like button. Looks very flat. Is there someway to put border or make it stand like a button?
我正在为 iOS7 准备我的应用程序。我进行了转换并正在与用户合作。应用程序中的按钮看起来不像按钮。看起来非常平坦。有没有办法放置边框或让它像按钮一样站立?
采纳答案by Nikola Kirev
The design principles in iOS7 have changed. However, if you want to go flatter, but still want a custom button that "stands like a button", you can try out this open source component collection:
iOS7 中的设计原则已经改变。但是,如果你想要更扁平化,但仍然想要一个“像按钮一样站立”的自定义按钮,你可以试试这个开源组件集合:
回答by Mehul Thakkar
Try this for adding border, It will work
试试这个来添加边框,它会起作用
#import <QuartzCore/QuartzCore.h>
then in viewDidLoad
然后在 viewDidLoad
_btn.layer.borderWidth=1.0f;
_btn.layer.borderColor=[[UIColor blackColor] CGColor];
_btn.layer.cornerRadius = 10;
also you can fill the color for making appearance somewhat like button, or best way is to use image there
您也可以填充颜色以使外观有点像按钮,或者最好的方法是在那里使用图像
Apart from BorderColor, you can do it by using Runtime attributes too.
除了 BorderColor 之外,您也可以使用运行时属性来实现。
回答by Shaik Riyaz
try this , this will set border to button
试试这个,这会将边框设置为按钮
#import <QuartzCore/QuartzCore.h>
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 10;
回答by mtisz
With Swift and XCode 6 you can do this.
使用 Swift 和 XCode 6,您可以做到这一点。
Click the UIButton element in Storyboard, and go to identity inspector. In the user defined runtime attributes, enter:
单击 Storyboard 中的 UIButton 元素,然后转到身份检查器。在用户定义的运行时属性中,输入:
layer.borderWidth number 1
If you want nice looking corners
如果你想要漂亮的角落
layer.cornerRadius number 5
layer.maskToBounds boolean true
Now this will give you a border but to set the colour you need to do it with code. Go to your view controller, and add an IBOutlet from your button. Note that in this case it's an IBOutlet, not an IBAction. Say you do,
现在这会给你一个边框,但要设置颜色,你需要用代码来做。转到您的视图控制器,并从您的按钮添加一个 IBOutlet。请注意,在这种情况下,它是 IBOutlet,而不是 IBAction。说你做,
@IBOutlet weak var xButton: UIButton!
Call this in the viewDidLoad function like below to set the colour.
在 viewDidLoad 函数中调用它,如下所示来设置颜色。
xButton.layer.borderColor = UIColor.whiteColor().CGColor
Thanks!
谢谢!
回答by Chris Alan
There are two way to simplify button in ios 7
ios 7 中有两种简化按钮的方法
1>Set image : Just like Button using setImage Property for button
1>设置图像:就像Button使用setImage属性为按钮
2>Set bordercolor borderWidth :
2>设置边框颜色边框宽度:
button.layer.borderWidth=1.0f;
button.layer.borderWidth=1.0f;
button.layer.borderColor=[[UIColor blackColor] CGColor];
button.layer.borderColor=[[UIColor blackColor] CGColor];
回答by cat
I suggest you try one of the iOS BootstrapButton libraries (or clones). Just change UIButton in the storyboard to BButton. There is just no preview in the storyboard.
我建议您尝试使用 iOS BootstrapButton 库(或克隆)之一。只需将故事板中的 UIButton 更改为 BButton。故事板中没有预览。
回答by Nora Alnashwan
If you use a background image, move button backward. Editor>Arrange>send backward
如果您使用背景图像,请向后移动按钮。编辑器>排列>向后发送
回答by monkeyMoo
In ios7 buttons are supposed to look borderless. Please see apple's View Transition Guidelinesfor help.
在 ios7 中按钮应该看起来无边框。请参阅 Apple 的View Transition Guidelines寻求帮助。
If you REALLY want the buttons to have a border, do: [button setImage:forState:]
to set a customized button image with border.
如果您真的希望按钮有边框,请执行以下操作:[button setImage:forState:]
设置带边框的自定义按钮图像。