0

I am creating my buttons (and other objects) programatically, as that is what I prefer. See below example:

let loginButton: UIButton = UIButton( frame: CGRect( x: 0, y: 0, width: UIScreen.mainScreen().bounds.width / 1.5, height: 50 ) )

Here I create a button that is screen width / 1.5 wide and 50px heigh. This size is perfect for an iPhone 4s resolution and all other devices, horizontally, as it takes the screen width into account. However, naturally, when running on a larger resolution device like the iPhone 6/6+, the buttons are still 50px tall, which is small and leaving a lot of content space empty.

I am sure there is a way to probe the content scale programatically so I can calculate the correct height for my buttons, depending on the device size.

I'm fairly new to Apple's Api's and a shove in the right direction, or a small example would be great.

PS: I want to continue creating my interfaces programatically, not using interface builder. So please refrain from suggesting anything of that sort.

Thanks in advance.

PersuitOfPerfection
  • 1,009
  • 1
  • 15
  • 28
  • 1
    Take a look into tutorials for AutoLayout. That should be the way to go. It can all be done in code or in InterfaceBuilder. – zisoft Oct 22 '14 at 12:54
  • @zisoft thanks for the reply, but seriously, all this just for one object? https://gist.github.com/anonymous/35d9c9fdb4825bce991f - Hopefully that is wrong and there is a much more condensed approach to this. Thanks – PersuitOfPerfection Oct 22 '14 at 13:53
  • Also, is anyone doing manual scaling? I presume some people prefer to use their own scaling methods. Thanks – PersuitOfPerfection Oct 23 '14 at 09:42
  • Really, it looks oversized at a first glance. But AutoLayout does the magic for differenct screen sizes, rotating the device and so on without the need to implement functions for that. Just set up the proper constraints and you are gone. – zisoft Oct 23 '14 at 09:48
  • I tried AutoLayout but my elements (buttons) were not being scaled up for the higher resolution devices? (using interface builder). For instance, they look fine on the iPhone 4s, but there is a huge area of unfilled content on a 6/6+ (due to the buttons not scaling up). – PersuitOfPerfection Oct 23 '14 at 10:06
  • check this solution for **Swift 4.2** [button with dynamic height programmically](https://stackoverflow.com/a/54879007/9614722) – moraei Feb 26 '19 at 05:40

0 Answers0