[英]Programmatically Setting Corner Radius for UIButtons in Landscape and Portrait Mode for All iPhone and iPad Devices
The app I am working on is a near replicate of Apple's default iPhone calculator app . 我正在使用的应用程序几乎是苹果默认iPhone计算器应用程序的复制品。
I am having a hard time in keeping the corner radius set for both the basic operator buttons in portrait and the basic operator buttons plus additional buttons in landscape mode. 我很难同时为纵向的基本操作员按钮和基本的操作员按钮以及横向模式下的其他按钮设置拐角半径。
Is specifying the corner radius (by dividing the UIButton.bounds.height / 2
) in viewWillLayoutSubviews()
the right place? 在
viewWillLayoutSubviews()
指定拐角半径(通过划分UIButton.bounds.height / 2
viewWillLayoutSubviews()
是否正确?
I need the buttons left and right side to stay round on all device rotations. 我需要左侧和右侧的按钮在所有设备旋转时保持圆角。 I am not concerned with the top and bottom of the buttons being round.
我不关心按钮的顶部和底部是圆形的。
Thank you all for your consideration in helping me figure out this issue. 谢谢大家在帮助我解决此问题方面的考虑。 I am not looking for a direct answer, but if you all could maybe point me to some topics to look into that would be a huge help.
我不是在寻找直接的答案,但是如果大家都可以指出一些话题,那将是巨大的帮助。 I have also tried setting the corner radius in
viewDidAppear().
我也尝试过在
viewDidAppear().
设置拐角半径viewDidAppear().
Looking for a programmatic solution using UIButton.bounds.height.
寻找使用
UIButton.bounds.height.
的编程解决方案UIButton.bounds.height.
I believe I am just trying to find the right method to implement the corner radius through 我相信我只是在尝试找到正确的方法来通过
var cornerRadius_forButtons: Double = 0
if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft{
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
}
else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight{
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
} else {
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
}
UIButton.layer.cornerRadius = CGFloat(cornerRadius_forButtons)
When you use this method the only thing that is slightly hard is setting the constraints. 使用此方法时,唯一困难的一点是设置约束。 You will not have to mess with code for the most part.
在大多数情况下,您不必弄乱代码。
1.) In an image editor make a circle. 1.)在图像编辑器中画一个圆圈。 Link To Image Editor I Used
链接到我使用的图像编辑器
2.) Add the operator/number to middle of circle 2.)将运算符/数字添加到圆圈的中间
3.) Make background transparent (Not needed if background white) 3.)使背景透明(如果背景为白色则不需要)
4.) Add the image to your apps Assets Folder 4.)将图像添加到您的应用程序资产文件夹
5.) In Xcode set the buttons image
property to the image you just created. 5.)在Xcode中,将按钮
image
属性设置为刚创建的图像。
6.) Create constraints for all of your buttons 6.)为所有按钮创建约束
Now it should work on all devices regardless of orientation. 现在,无论方向如何,它都可以在所有设备上运行。
Issue was on my end. 问题就在我身上。 The solution to getting round edges on both the left and ride side of UIButton(s) in portrait and landscape modes for all iOS devices was successfully done by calculating and implementing the corner radius in
通过在iOS中计算和实现拐角半径,成功完成了在所有iOS设备的纵向和横向模式下在UIButton的左侧和行驶侧获得圆形边缘的解决方案。
var cornerRadius_forButtons: Double = 0
override func viewWillLayoutSubviews() {
if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft{
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
}
else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight{
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
} else {
//PORTRAIT
cornerRadius_forButtons = Double(tag1_Button.bounds.height / 2.0)
}
tag1_Button.layer.cornerRadius = CGFloat(cornerRadius_forButtons)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.