简体   繁体   中英

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 .

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?

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().

Looking for a programmatic solution using 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. Link To Image Editor I Used

建立的圈子

2.) Add the operator/number to middle of circle 在此处输入图片说明

3.) Make background transparent (Not needed if background white)

4.) Add the image to your apps Assets Folder

在此处输入图片说明

5.) In Xcode set the buttons image property to the image you just created.

在此处输入图片说明

6.) Create constraints for all of your buttons

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

   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)

     } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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