ios 如何在 Storyboard 中制作一个简单的圆形按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38874517/
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 make a simple rounded button in Storyboard?
提问by Gintas_
I just started learning iOS development, cannot find how to make simple rounded button. I find resources for old versions. Do I need to set a custom background for a button? In Android, I would just use a 9 patch, but I know iOS does not have this capability.
我刚开始学习 iOS 开发,找不到如何制作简单的圆形按钮。我找到了旧版本的资源。我需要为按钮设置自定义背景吗?在 Android 中,我只会使用 9 补丁,但我知道 iOS 没有此功能。
采纳答案by realtimez
To do it in the storyboard, you need to use an image for the button.
要在故事板中执行此操作,您需要为按钮使用图像。
Alternatively you can do it in code:
或者,您可以在代码中执行此操作:
btn.layer.cornerRadius = 10
btn.clipsToBounds = true
回答by Nishant
Short Answer: YES
简答:是
You can absolutely make a simple rounded button without the need of an additional background image or writing any code for the same. Just follow the screenshot given below, to set the runtime attributes for the button, to get the desired result.
您绝对可以制作一个简单的圆形按钮,而无需额外的背景图像或为此编写任何代码。只需按照下面给出的屏幕截图设置按钮的运行时属性,即可获得所需的结果。
It won't show in the Storyboard
but it will work fine when you run the project.
它不会显示在 中,Storyboard
但是当您运行项目时它会正常工作。
Note:
The 'Key Path'layer.cornerRadius
and value is 5. The value needs to be changed according to the height and width of the button. The formula for it is the height of button * 0.50. So play around the value to see the expected rounded button in the simulator or on the physical device. This procedure will look tedious when you have more than one button to be rounded in the storyboard.
注意:
该“关键路径”layer.cornerRadius
和值是5的值需要根据按钮的高度和宽度被改变。它的公式是按钮的高度 * 0.50。因此,在模拟器或物理设备上查看该值以查看预期的圆形按钮。当故事板中有多个按钮需要四舍五入时,此过程将看起来很乏味。
回答by ChikabuZ
You can do something like this:
你可以这样做:
@IBDesignable class MyButton: UIButton
{
override func layoutSubviews() {
super.layoutSubviews()
updateCornerRadius()
}
@IBInspectable var rounded: Bool = false {
didSet {
updateCornerRadius()
}
}
func updateCornerRadius() {
layer.cornerRadius = rounded ? frame.size.height / 2 : 0
}
}
Set class to MyButton
in Identity Inspector
and in IB you will have rounded
property:
将 class 设置为MyButton
inIdentity Inspector
和 in IB,您将拥有以下rounded
属性:
回答by anson
- Create a Cocoa Touch class.
- 创建一个 Cocoa Touch 类。
Insert the code in RoundButton class.
import UIKit @IBDesignable class RoundButton: UIButton { @IBInspectable var cornerRadius: CGFloat = 0{ didSet{ self.layer.cornerRadius = cornerRadius } } @IBInspectable var borderWidth: CGFloat = 0{ didSet{ self.layer.borderWidth = borderWidth } } @IBInspectable var borderColor: UIColor = UIColor.clear{ didSet{ self.layer.borderColor = borderColor.cgColor } } }
Refer the image.
在 RoundButton 类中插入代码。
import UIKit @IBDesignable class RoundButton: UIButton { @IBInspectable var cornerRadius: CGFloat = 0{ didSet{ self.layer.cornerRadius = cornerRadius } } @IBInspectable var borderWidth: CGFloat = 0{ didSet{ self.layer.borderWidth = borderWidth } } @IBInspectable var borderColor: UIColor = UIColor.clear{ didSet{ self.layer.borderColor = borderColor.cgColor } } }
参考图片。
回答by FrankkieNL
I found the easiest way to do this, is by setting the cornerRadius to half of the height of the view.
我发现最简单的方法是将cornerRadius 设置为视图高度的一半。
button.layer.cornerRadius = button.bounds.size.height/2
回答by Joseph Shenton
As other answer have suggested to perform most of this work in code only one answer actually provided a way to view your changes in the storyboard IB Interface. My answer goes beyond that answer by allowing you to change the cornerRadius of the view, button, image, etc.
正如其他答案建议在代码中执行大部分工作一样,只有一个答案实际上提供了一种在故事板 IB 界面中查看更改的方法。我的答案超越了那个答案,允许您更改视图、按钮、图像等的角半径。
Please take a look at the following code. To use this code create a new swift file called RoundedView or whatever you would like to call it then go to your storyboard and change the class to either "RoundedView", "RoundedImageView" or "RoundedButton".
请看下面的代码。要使用此代码,请创建一个名为 RoundedView 或任何您想调用的新 swift 文件,然后转到您的故事板并将类更改为“RoundedView”、“RoundedImageView”或“RoundedButton”。
import UIKit
@IBDesignable class RoundedImage: UIImageView
{
override func layoutSubviews() {
super.layoutSubviews()
updateCornerRadius()
}
@IBInspectable var rounded: Bool = false {
didSet {
updateCornerRadius()
}
}
@IBInspectable var cornerRadius: CGFloat = 0.1 {
didSet {
updateCornerRadius()
}
}
func updateCornerRadius() {
layer.cornerRadius = rounded ? cornerRadius : 0
layer.masksToBounds = rounded ? true : false
}
}
@IBDesignable class RoundedView: UIView
{
override func layoutSubviews() {
super.layoutSubviews()
updateCornerRadius()
}
@IBInspectable var rounded: Bool = false {
didSet {
updateCornerRadius()
}
}
@IBInspectable var cornerRadius: CGFloat = 0.1 {
didSet {
updateCornerRadius()
}
}
func updateCornerRadius() {
layer.cornerRadius = rounded ? cornerRadius : 0
layer.masksToBounds = rounded ? true : false
}
}
@IBDesignable class RoundedButton: UIButton
{
override func layoutSubviews() {
super.layoutSubviews()
updateCornerRadius()
}
@IBInspectable var rounded: Bool = false {
didSet {
updateCornerRadius()
}
}
@IBInspectable var cornerRadius: CGFloat = 0.1 {
didSet {
updateCornerRadius()
}
}
func updateCornerRadius() {
layer.cornerRadius = rounded ? cornerRadius : 0
layer.masksToBounds = rounded ? true : false
}
}
回答by Ketan Parmar
You can connect IBOutlet
of yur button from storyboard.
您可以IBOutlet
从故事板连接您的按钮。
Then you can set corner radius
of your button to make it's corner round.
然后你可以设置corner radius
你的按钮,使它的角变圆。
for example, your outlet
is myButton
then,
例如,你outlet
是myButton
那么,
Obj - C
对象-C
self.myButton.layer.cornerRadius = 5.0 ;
Swift
迅速
myButton.layer.cornerRadius = 5.0
If you want exact round button then your button's width
and height
must be equal
and cornerRadius
must be equal to height or width / 2 (half of the width or height).
如果你想确切的圆形按钮,然后你的按钮的width
和height
必须equal
和cornerRadius
必须等于(宽度或高度的一半)的高度或宽度/ 2。
回答by venki mohan
While adding layer.cornerRadius
in the storyboard make sure that you don't have leading or trailing spaces. If you do copy paste, you might get spaces inserted. Would be nice if XCode say some kind of warning or error.
在添加layer.cornerRadius
故事板时,请确保您没有前导或尾随空格。如果你复制粘贴,你可能会插入空格。如果 XCode 说某种警告或错误,那就太好了。
回答by Sanjeet verma
Try this!!
尝试这个!!
override func viewDidLoad() {
super.viewDidLoad()
var button = UIButton.buttonWithType(.Custom) as UIButton
button.frame = CGRectMake(160, 100, 200,40)
button.layer.cornerRadius =5.0
button.layer.borderColor = UIColor.redColor().CGColor
button.layer.borderWidth = 2.0
button.setImage(UIImage(named:"Placeholder.png"), forState: .Normal)
button.addTarget(self, action: "OnClickroundButton", forControlEvents: .TouchUpInside)
button.clipsToBounds = true
view.addSubview(button)
}
func OnClickroundButton() {
NSLog(@"roundButton Method Called");
}
回答by muhammad usman
The extension is the best option for this problem. Create an extension of View or Button
扩展是解决这个问题的最佳选择。创建 View 或 Button 的扩展
public extension UIView {
//Round the corners
func roundCorners(){
let radius = bounds.maxX / 16
layer.cornerRadius = radius
}
}
Call it from the code
从代码中调用
button.roundCorners()