简体   繁体   English

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

[英]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.

相关问题 Ipad在横向模式下和iphone在纵向模式下 - Ipad in landscape mode and iphone in portrait mode 动态角半径为景观和肖像 - Dynamic corner Radius for landscape and portrait 应用程序支持ipad和iphone中的应用程序横向和纵向模式 - App support the app Landscape and portrait mode in both ipad and iphone iPhone应用程序在横向和纵向模式下都可以正常工作,但在iPad上只能在一种模式下工作? - Iphone app is working fine in both landscape and portrait mode but in ipad it only works in one mode? iPad 1显示纵向模式而不是横向 - iPad 1 showing portrait mode instead of Landscape 在纵向和横向模式下为iPad设置不同的约束 - Set different constraints for iPad in portrait and landscape mode 启动应用程序Iphone纵向和Ipad横向 - Launch app Iphone portrait, and Ipad landscape iPhone / iPad视点在纵向和横向上发生变化 - iPhone/iPad viewpoint changes on portrait and landscape iPad兼容模式下的iPhone应用程序在横向上有奇怪的布局(我强迫肖像) - iPhone app on iPad compatibility mode has strange layout in landscape (while I force portrait) 支持iPhone上的纵向方向的通用应用程序和iPad上的横向+纵向 - Supporting Universal App with Portrait Orientation on iPhone and Landscape+Portrait on iPad
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM