Get the Same UI for All iPhone Screens Using NSLayoutConstraint Scaling | HackerNoon

South Africa News News

Get the Same UI for All iPhone Screens Using NSLayoutConstraint Scaling | HackerNoon
South Africa Latest News,South Africa Headlines
  • 📰 hackernoon
  • ⏱ Reading Time:
  • 72 sec. here
  • 3 min. at publisher
  • 📊 Quality Score:
  • News: 32%
  • Publisher: 51%

'Get the Same UI for All iPhone Screens Using NSLayoutConstraint Scaling' ios uikit

Often the UI of iOS applications is only designed for screens of one size. For example, iPhone X or iPhone 13 , and then iOS developers make designs for screens of other sizes.

With large screens, everything is simple. But placing the design of the iPhone X on the screen of the iPhone SE 2016 is more difficult, some elements and indents have to be reduced manually.🙂 Result with UI scale:We go through the UIView hierarchy and scale all the constraints. extension UIView { func callRecursively -> Void) { body subviews.forEach { $0.callRecursively } } func scale { // 390 is an iPhone 13 screen width var scaleFactor=min callRecursively { in // to avoid scalе of system constraints guard level !=0 else { return } for constraint in subview.constraints { if constraint.constant !=0 { constraint.constant=CGFloat * scaleFactor } } } } }

class ViewController: UIViewController { override func viewDidLoad { super.viewDidLoad // one line is enough view.scale } }func scale { // 390 is an iPhone 13 screen width // 375 is for iPhone Х // and 320 is for iPhone SE 2016 var scaleFactor=min if let kFor5=kFor5, UIScreen.main.bounds.width <=320 { scaleFactor=scaleFactor * kFor5 } else if let kFor8=kFor8, UIScreen.main.bounds.width <=375, UIScreen.main.bounds.

class ViewController: UIViewController { override func viewDidLoad { super.viewDidLoad // you can play with those factors for best results view.scale } }That's all. Please write if you have any comments, I'm almost sure it can be improved further. Also, please let me know if you have solved a similar problem.

We have summarized this news so that you can read it quickly. If you are interested in the news, you can read the full text here. Read more:

hackernoon /  🏆 532. in US

South Africa Latest News, South Africa Headlines



Render Time: 2025-03-11 01:02:06