Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带约束的快速圆形按钮_Ios_Iphone_Swift_Ios9 - Fatal编程技术网

Ios 带约束的快速圆形按钮

Ios 带约束的快速圆形按钮,ios,iphone,swift,ios9,Ios,Iphone,Swift,Ios9,在iOS9iPhone应用程序中,我有一个按钮,我希望它有圆角,所以它是完全圆角的。我在我的故事板上添加了一个按钮,并添加了两个约束以使其水平和垂直居中。在我添加了几行代码将圆角应用到按钮之前,这一切都是有效的。这是我的故事板:然后我将以下代码行添加到我的视图控制器: startButton.backgroundColor = UIColor.clearColor() startButton.layer.cornerRadius = 75 startButton.layer.borderWidt

在iOS9iPhone应用程序中,我有一个按钮,我希望它有圆角,所以它是完全圆角的。我在我的故事板上添加了一个按钮,并添加了两个约束以使其水平和垂直居中。在我添加了几行代码将圆角应用到按钮之前,这一切都是有效的。这是我的故事板:然后我将以下代码行添加到我的视图控制器:

startButton.backgroundColor = UIColor.clearColor()
startButton.layer.cornerRadius = 75
startButton.layer.borderWidth = 2
startButton.layer.borderColor = UIColor.whiteColor().CGColor
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    startButton.backgroundColor = UIColor.clearColor()
    startButton.layer.cornerRadius = startButton.bounds.size.height / 2
    startButton.layer.borderWidth = 2
    startButton.layer.borderColor = UIColor.whiteColor().CGColor
}
当我运行这是一个iPhone 5s模拟器时,边界混乱,如下所示:
几天来,我一直在尝试将按钮完全四舍五入,但没有结果。我能做什么?

似乎
拐角半径大于其高度。在视图控制器中尝试以下代码:

startButton.backgroundColor = UIColor.clearColor()
startButton.layer.cornerRadius = 75
startButton.layer.borderWidth = 2
startButton.layer.borderColor = UIColor.whiteColor().CGColor
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    startButton.backgroundColor = UIColor.clearColor()
    startButton.layer.cornerRadius = startButton.bounds.size.height / 2
    startButton.layer.borderWidth = 2
    startButton.layer.borderColor = UIColor.whiteColor().CGColor
}

似乎
拐角半径
大于其高度。在视图控制器中尝试以下代码:

startButton.backgroundColor = UIColor.clearColor()
startButton.layer.cornerRadius = 75
startButton.layer.borderWidth = 2
startButton.layer.borderColor = UIColor.whiteColor().CGColor
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    startButton.backgroundColor = UIColor.clearColor()
    startButton.layer.cornerRadius = startButton.bounds.size.height / 2
    startButton.layer.borderWidth = 2
    startButton.layer.borderColor = UIColor.whiteColor().CGColor
}

首先,使按钮完全呈方形,然后将其拐角旋转一半宽度或高度

例如:

//200*200方形按钮
startButton.frame.size=CGSize(宽:200,高:200)
startButton.backgroundColor=UIColor.clearColor()
//把角做成圆形
startButton.layer.cornerRadius=startButton.frame.size.height/2
startButton.layer.borderWidth=2

startButton.layer.borderColor=UIColor.whiteColor().CGColor
首先使按钮完全呈方形,然后将其拐角处旋转一半宽度或高度

例如:

//200*200方形按钮
startButton.frame.size=CGSize(宽:200,高:200)
startButton.backgroundColor=UIColor.clearColor()
//把角做成圆形
startButton.layer.cornerRadius=startButton.frame.size.height/2
startButton.layer.borderWidth=2

startButton.layer.borderColor=UIColor.whiteColor().CGColor
完全以编程方式创建它怎么样:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let button = UIButton(type: .Custom)
    button.frame = CGRectMake(160, 100, 50, 50) // (x, y, width, height)
    button.backgroundColor = UIColor.redColor()
    button.layer.cornerRadius = 0.5 * button.bounds.size.width
    button.layer.borderWidth = 2
    button.layer.borderColor = UIColor.whiteColor().CGColor
    button.setTitle("Hi!", forState: .Normal)

    button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)


    view.addSubview(button)
}

func buttonAction(sender: UIButton!) {
    print("hello!")
}
显然,这些定制中有很多是可选的(例如背景色),但只是为了向您展示,您仍然可以使用按钮完成任何您想做的事情


完全以编程方式创建它怎么样:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let button = UIButton(type: .Custom)
    button.frame = CGRectMake(160, 100, 50, 50) // (x, y, width, height)
    button.backgroundColor = UIColor.redColor()
    button.layer.cornerRadius = 0.5 * button.bounds.size.width
    button.layer.borderWidth = 2
    button.layer.borderColor = UIColor.whiteColor().CGColor
    button.setTitle("Hi!", forState: .Normal)

    button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)


    view.addSubview(button)
}

func buttonAction(sender: UIButton!) {
    print("hello!")
}
显然,这些定制中有很多是可选的(例如背景色),但只是为了向您展示,您仍然可以使用按钮完成任何您想做的事情


你可以这样做:
yourButton.frame=CGRectMake(100,100,75,75)yourButton.layer.cornerRadius=yourButton.bounds.size.width/2.0 yourButton.layer.masksToBounds=true
我删除了cornerRadius并将masksToBounds设置为true,现在按钮周围有了一个方形边框。现在我明白了为什么我会得到第二张截图上的效果。按钮大小为150x150,但边框显示在文本周围,而不是150x150周围。如何使边框显示在150x150周围?根据询问者的请求关闭。您可以尝试以下操作:
yourButton.frame=CGRectMake(100,100,75,75)yourButton.layer.cornerRadius=yourButton.bounds.size.width/2.0 yourButton.layer.masksToBounds=true
我删除了cornerRadius并将masksToBounds设置为true,现在我在按钮周围有了一个方形边框。现在我明白了为什么我会得到第二张截图上的效果。按钮大小为150x150,但边框显示在文本周围,而不是150x150周围。如何使边框显示在150x150周围?根据询问者的请求关闭。