Matthew Cho
Matthew Cho

Reputation: 103

How to have different layouts for iPhone and iPad (Swift)

In an app that I use, there are 4 rows of buttons with each row containing 3 buttons. When I use the same app on an iPad, the alignment of the buttons becomes 2 rows containing 6 buttons each. Also, the width and height of the buttons change depending on the screen size. I want my app to have the same features but I don't know how to do it. Any help?

Upvotes: 3

Views: 10069

Answers (3)

Shivam Tripathi
Shivam Tripathi

Reputation: 1493

Use Autolayout for defining constraints on buttons. There might be two cases :

  • Either you want your buttons to be of the same height/width; In this case, you can define the height and width constraints to your buttons.
  • Or you want the buttons to be bigger. For that, you can define autolayout constraints in code if the screen size is for iPad.

Upvotes: 0

Sarabjit Singh
Sarabjit Singh

Reputation: 1824

You need to use size classes. Following is the link which helps you to understand size classes :

Size Classes or Adaptive layout

Upvotes: 1

user2094867
user2094867

Reputation: 319

1) You can use Autolayout constraints and size classes to make the view compatible to both iPhone and iPad screen sizes.

2) You can create and use separate Storyboards or .Xibs each for iPhone and iPad and use accordingly.

3) if the view does not have complex UI, then you can simply manage by doing it via code by detecting the view size, width height etc i.e whether it is iphone or iPad and then setting the frame, width, height of the UIElements like button, label, image view etc.

Upvotes: 5

Related Questions