Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a UIButton a % of the screen size

I noticed certain button sizes look great on one the iPhone 5 simulator but do not look as good on the iPhone 6 simulator and this is because the heights or constraints that I place on the UIButtons end up leaving a lot of blank space down the bottom of my App Screen.

I would like to have a button that is 40% of the screen size regardless of what device I am simulating on.

Any ideas on how to make the size of the button stay 40% of the screen size regardless of the device?

like image 516
Jack Avatar asked Jun 25 '15 07:06

Jack


2 Answers

  1. Ctrl drag from button to superview and select Equal Widths
  2. Open Size Inspector edit Equal Widths constraint and set multiplier to 0.4. And you will see something like this: enter image description here

  3. Add missing constraints and update frames.

enter image description here

like image 65
ChikabuZ Avatar answered Nov 18 '22 08:11

ChikabuZ


You can't set a constraint to be 40% of the screen height. However, if the button always has a 20px leading and trailing to the superview you could use that width and set an aspect ratio height.

Another way could be to use UIScreen.mainScreen().bounds.size.height * 0.4 for your button height.

A third way is to use the button's superview to set the height. Assuming that your button is a direct child of a UIViewController's view: view.bounds.size.height * 0.4.

There's probably a bunch of other ways to do this as well but none of them will involve setting the height to an actual percentage as far as I'm aware.

like image 1
donnywals Avatar answered Nov 18 '22 09:11

donnywals