繁体   English   中英

以编程方式为所有iPhone和iPad设备在横向和纵向模式下为UIButton设置转角半径

[英]Programmatically Setting Corner Radius for UIButtons in Landscape and Portrait Mode for All iPhone and iPad Devices

我正在使用的应用程序几乎是苹果默认iPhone计算器应用程序的复制品。

我很难同时为纵向的基本操作员按钮和基本的操作员按钮以及横向模式下的其他按钮设置拐角半径。

viewWillLayoutSubviews()指定拐角半径(通过划分UIButton.bounds.height / 2 viewWillLayoutSubviews()是否正确?

我需要左侧和右侧的按钮在所有设备旋转时保持圆角。 我不关心按钮的顶部和底部是圆形的。

谢谢大家在帮助我解决此问题方面的考虑。 我不是在寻找直接的答案,但是如果大家都可以指出一些话题,那将是巨大的帮助。 我也尝试过在viewDidAppear().设置拐角半径viewDidAppear().

寻找使用UIButton.bounds.height.的编程解决方案UIButton.bounds.height. 我相信我只是在尝试找到正确的方法来通过

   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)

使用此方法时,唯一困难的一点是设置约束。 在大多数情况下,您不必弄乱代码。

1.)在图像编辑器中画一个圆圈。 链接到我使用的图像编辑器

建立的圈子

2.)将运算符/数字添加到圆圈的中间 在此处输入图片说明

3.)使背景透明(如果背景为白色则不需要)

4.)将图像添加到您的应用程序资产文件夹

在此处输入图片说明

5.)在Xcode中,将按钮image属性设置为刚创建的图像。

在此处输入图片说明

6.)为所有按钮创建约束

现在,无论方向如何,它都可以在所有设备上运行。

问题就在我身上。 通过在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM