How to manage view when model the same but different views?

Hi,

What is the best architecture/design approach for SwiftUI app where we have same data but could be slightly different or totally different views for iPhone and iPad?

How to avoid code redundant?

If there is some example I would really appreciate.

Thanks.

MVVM, where:

  • Model is value types (structs)
  • Views are value types (SwiftUI Views)
  • ViewModel is a reference type (a class), so it can be passed around the app, and an ObservableObject (so Views can respond to changes)

There is nothing s powerful as Autolayout.

Depending on what you need, Spacer may provide some solution

https://stackoverflow.com/questions/62800463/correct-way-to-layout-swiftui-similar-to-autolayout

For more sophisticated needs, you'll have to use GeometryReader and rebuild some how AUtolayout by yourself: See example here for Portrait / Landscape, that should work also for iPhone / iPad:

https://stackoverflow.com/questions/67187484/autolayout-in-swiftui

If the views are really different, best would be to create 2 views and call the right one depending on userInterface idiom:

https://stackoverflow.com/questions/57652242/how-to-detect-whether-targetenvironment-is-ipados-in-swiftui

if UIDevice.current.localizedModel == "iPhone" {

} else if UIDevice.current.localizedModel == "iPad" {

}
How to manage view when model the same but different views?
 
 
Q