Working with frames instead of autolayout

Hi everyone,


so I haven’t understood one thing:

Basically, I don’t use the storyboard to create the user interface since for complex UIs like having multiple subviews in one container view etc. using the storyboard is a pain.


I am using the good old way with the frames. First I created a template for the iPhone 8 screen using Adobe CC, then I started coding the UI in Xcode by having a look at the frames. For example, I am creating a label like this:


  let screenSize = UIScreen.main.bounds
  let width = screenSize.width
  let height = screenSize.height

  let accountTypeLabel = UILabel()
  accountTypeLabel.frame = CGRect(x: width * (53 / width), y: height * (172 / height), width: width * (269 / width), height: height * (37 / height))
  accountTypeLabel.text = ACCOUNT_TYPE_LABEL
  accountTypeLabel.textColor = UIColor.white
  accountTypeLabel.backgroundColor = DARK_BLUE_COLOR
  accountTypeLabel.textAlignment = .center
  let accountTypeLabelLabelHeight = accountTypeLabel.frame.size.height
  accountTypeLabel.font = UIFont(name: "Kefa", size: accountTypeLabelLabelHeight * (22 / accountTypeLabelLabelHeight))

Depending on the iPhone device, we have different widths and heights of course. So I just put them into variables and define width and height of my label relatively to the screensize of the corresponding iPhone.

Well, this works great for iPhone 8. But I just tried to run the code for the iPhone 8 Plus and here, the label is a little bit more on the left part of the screen, so it doesn’t look the same as for iPhone 8.


Now my question, why is that the case? Since I am putting the height and width always relatively to the screensize, I would have expected to have some kind of an “autolayout” functionality


Can anyone help?



Powered by WPeMatico

You may also like...

Comments are closed.