-2

I have made an app, with two ViewControllers each displaying different buttons. On the first display controller, there is a Record Sound button, Record label and a stop Recording button. The Record label is 'attached' to the Record sound button and the Stop recording button is 'attached' to the label, all of this using constraints. The issue I am having is that the buttons stay the same size on different devices, causing a cluttered interface or an empty one, depending on the device. I have tried different fixes across the internet but they are only for one individual button, not for groups of buttons.

I would like to learn of a solution that would change the size of the button or stack view depending on the screen size

I also want to learn how to do this with the buttons in multiple stack views in my second ViewController.

Examples:

iPhone 4s/SE: the stop recording button gets cut off in landscape mode. The buttons in both views are two large compared to the size of the screen.

iPads: The buttons are too small compared to the size of the screen.

Here are the fixes I have looked at and tried:

How can I dynamically set the size of the imageView according to the size of the image

Adjust button sizes based on screen size

Imgur is rejecting my request so I am not able to upload the image of my problem.

JouMyu
  • 1
  • 3
  • Check this Answer and apply it to your UIButton in place of imageView. https://stackoverflow.com/a/36702170/4910767 – Badal Shah Aug 08 '18 at 09:06
  • Ok I'll tell you if it works. – JouMyu Aug 08 '18 at 13:14
  • Hi, sorry, i may have worded my question wrongly. The button frame changes but not the image of the button inside the frame, I wanted to learn how to increase the image of the button inside. – JouMyu Aug 09 '18 at 04:25

1 Answers1

0

Maybe you could try this, first detecting the size of the current screen in order to modify programmatically the size of your buttons to get a right position.

var heightScreen: CGFloat = 0
var widthScreen: CGFloat = 0

    override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)

         let bounds = UIScreen.main.bounds
         self.heightScreen = bounds.size.height //get the height of screen
         self.widthScreen = bounds.size.width //get the width of screen

         //once you have the sizes, you can validate them, for instance..

                //for iPhone 5/5s/SE
            if self.heightScreen==568.0 && self.widthScreen==320.0 { 
                 //here you can set the correct size to your button, depending of the iPhone's screen

                // for iPhone 6/6s/7/7s
            }else if self.heightScreen==667.0 && self.widthScreen==375.0{
                //here....

                //// for iPhone 6s Plus/ 7s Plus
            }else if self.heightScreen==736.0 && self.widthScreen==414.0{
                //here....

                //for iPad Mini / Air
            }else if self.heightScreen==1024.0 && self.widthScreen==768.0{
                //here....

                //iPad Pro
            }else if self.heightScreen==1366.0 && self.widthScreen==1024.0{
                //here....
            }

    }
Roy Bernal
  • 121
  • 3