如何制作圆角椭圆形按钮?

问题描述 投票:0回答:3

我的目标是让我的按钮看起来像这样:

enter image description here

减去按钮周围的黑边。

读了很多帖子后,我看到大多数解决方案都说要使用

layer.cornerRadius = 10.0

当我这样做时,我得到这个:

enter image description here

它使边缘变圆,但没有给我我想要的目标。

有什么建议吗?

ios swift button rounded-corners
3个回答
21
投票

斯威夫特3

myButton.layer.cornerRadius = myButton.frame.height / 2

myButton.backgroundColor = UIColor.blue
myButton.clipsToBounds = true
myButton.tintColor = UIColor.white
myButton.setTitle("Connect With Facebook", for: .normal)

enter image description here


2
投票

斯威夫特5

从 iOS 15 开始,您可以使用 UIButtonConfiguration 获得更好的外观:

var configuration = UIButton.Configuration.blue()
configuration.cornerStyle = .capsule
configuration.baseForegroundColor = .white
configuration.buttonSize = .large
configuration.title = "Connect With Facebook"

let button = UIButton(configuration: configuration, primaryAction: nil)

capsule
风格将为您提供比以前更平滑的舍入。


1
投票

你可以这样做(例如,如果你的按钮被称为按钮):

button.layer.cornerRadius = button.bounds.size.height / 2.0
© www.soinside.com 2019 - 2024. All rights reserved.