How do I design adaptive layouts for all size classes in SwiftUI/UIKit?

Designing adaptive layouts for all size classes in SwiftUI and UIKit requires understanding how to leverage the available tools and frameworks effectively. SwiftUI provides a declarative syntax and built-in support for adapting to different size classes, while UIKit offers more granular control through Auto Layout and size class checks. Below is an example of how to implement adaptive layouts in both frameworks.

SwiftUI Example

struct ContentView: View { var body: some View { VStack { if UIDevice.current.userInterfaceIdiom == .pad { Text("This is an iPad layout!") .font(.largeTitle) .padding() } else { Text("This is an iPhone layout!") .font(.headline) .padding() } } } }

UIKit Example

class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() adaptLayoutForSizeClass() } func adaptLayoutForSizeClass() { if traitCollection.horizontalSizeClass == .regular { // iPad layout let label = UILabel() label.text = "This is an iPad layout!" label.font = UIFont.systemFont(ofSize: 32) label.textAlignment = .center view.addSubview(label) } else { // iPhone layout let label = UILabel() label.text = "This is an iPhone layout!" label.font = UIFont.systemFont(ofSize: 16) label.textAlignment = .center view.addSubview(label) } } }

adaptive layouts SwiftUI UIKit size classes responsive design