damithH
damithH

Reputation: 5178

how to give two sizes(for iPhone and iPad ) for UIView using auto layout?

how to give sizes for view(height or width) for a iPhone and iPad separately using auto layout via interface builder ?

Upvotes: 0

Views: 621

Answers (1)

BlackWolf
BlackWolf

Reputation: 5629

This question is a bit old, but for everyone coming here, the answer is: You don't. If the reason wasn't very clear back in 2014 when this question was asked, it is very clear since iOS 9 and the introduction of Split View and Slide Over.

To be a bit more concrete:

  • Don't rely on separate storyboards or UIDevice.current.userInterfaceIdiom. This tells you if you are on an iPhone or iPad, but since an app can now be in split view, it doesn't give you ANY information about available screen real estate
  • Don't rely on UIScreen.main.bounds - if an app is in split view, the screen bounds might not equal your app's size, so this information is useless for layout decisions
  • Don't blindly rely on self.view.bounds in your view controller. If the user moves the app into split view, the size of your view might change at any time. You CAN use this if you're monitoring the bounds (e.g. by overriding bounds and using didSet), but I would argue there is almost always a better way, and this doesn't help you if you are not in a view controller.

Apple recommends using size classes. While this does seem a bit limiting at first and takes some getting used to, almost all layout decisions these days can be solved using size classes and proper auto layout. The great thing about size classes is that, if the size class changes, for example because an app moves into split view, the system will automatically take care of removing the auto layout constraints of old size class and adding the appropriate ones for the new size class.

Upvotes: 1

Related Questions