[英]Preserve round edges of UIButton
trying to preserve the 'roundness' of a UIButton while user rotates the device:尝试在用户旋转设备时保留 UIButton 的“圆度”:
func resizeButtons() {
startButton.layer.masksToBounds = true
startButton.layer.cornerRadius = self.startButton.frame.size.width / 2
startButton.layer.borderColor = UIColor.blackColor().CGColor
startButton.layer.borderWidth = 1.0
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
switch UIDevice.currentDevice().orientation{
case .Portrait:
self.resizeButtons()
case .PortraitUpsideDown:
self.resizeButtons()
case .LandscapeLeft:
self.resizeButtons()
case .LandscapeRight:
self.resizeButtons()
default:
print("default")
}
}
I am using auto layout and StoryBoards (button aspect ratio 1:1, and aspect ratio button to container view 1:3, button center horizontally and vertically to container).我正在使用自动布局和 StoryBoards(按钮纵横比 1:1,纵横比按钮到容器视图 1:3,按钮中心水平和垂直到容器)。
The result is a bit cumbersome and 'ugly' as the device rotates the UIButton becomes a rounded corner rectangle and then full circle.结果有点麻烦和“丑陋”,因为设备旋转 UIButton 变成圆角矩形,然后变成完整的圆形。
Have you tried just setting the cornerRadius to a static variable?您是否尝试将cornerRadius 设置为静态变量? Your frame width changes every time you rotate, causing it to become either a circle or a rounded rectangle.
每次旋转时,您的框架宽度都会发生变化,使其变成圆形或圆角矩形。 My preferred cornerRadius value is 5, eg
button.layer.cornerRadius = 5
我首选的cornerRadius 值为5,例如
button.layer.cornerRadius = 5
I also wouldn't call the resize every time the orientation changes.每次方向改变时,我也不会调用调整大小。 Just set the variables in viewDidLoad() and call it a day
只需在 viewDidLoad() 中设置变量并调用它
EDIT: For a circle:编辑:对于一个圆圈:
That is the UIButton.那就是 UIButton。 It is centered in the view, but that shouldn't matter.
它以视图为中心,但这无关紧要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.