Keep single storyboard for iPhone and iPad - ios

I am creating a demo project for keeping single storyboard for both iPad and iPhone devices. I am going to use Xcode 8 first time.
I created a button and put this button in center of the View with using constraints. I successfully managed the width and height of the button according to the device. On iPhone width is 100 and height is 50. And on iPad width is 200 and height is 100. To achieve this I used "Vary for Traits".
So I just want to know, am I on correct flow, I mean, to achieve size classes "Vary for Traits" is the correct ways OR is there any other way to achieve this?

Related

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?
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.
You need to use size classes. Following is the link which helps you to understand size classes :
Size Classes or Adaptive layout
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.

Size classes and storyboard in UITableView

I need to add 3 labels to my UITableViewCell when my phone is in portrait, but when it's in landscape mode it should display 5 labels. Just to add complexcity when same cell is in ipad landscape it should show 6 labels. It should look something like this.
iPhone portrait
iPhone landscape
iPad landscape
I can do this using diffrent UITableViewCell XIB's but I want do this using Size classes and storyboard. Is there any example / tutorial / suggestion to follow ?
What you're looking for is this tutorial by Apple:
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/EnableAndDisableViews.html
This panel will install a given view for a given size class, in other words, make sure it exists or doesn't exist:
(source: apple.com)
This still doesn't give you the opportunity to change it for iPad specifically because the size classes aren't distinguishable but perhaps you could smuggle as I think the iPhone has it's height as Compact in that mode and the iPad and iPhone 6(S) Plus as Regular. So you need to add that extra label in the width looking at the combination of Regular width and height.
Also make sure you install the right layout constraints in more or less the same way as in one scenario you space to the next label while in the next scenario that label is gone and you need to space it to the right side.

Centering in Storyboard

I am a little bit confused by Xcode 6 Storyboard.
The auto layout seems to be a very powerful tool to build UI for different devices. However, no matter what kinds of layout I chose (compact or any height/width) or even uncheck the "Use auto layout" and "Use size classes", what I mapped in the storyboard view always looks different when I am testing (using an iPhone 6).
For example if I left a UIButton at the center of the storyboard, it will be a little right down away from center when appears on my iPhone 6.
Could anyone please give me some help on this. Maybe just for iPhone6 instead of different devices. Thank you very much.
If you simply want to center an UI element check both Horizontal Center in Container and Vertical Center in Container in the storyboard constraints alignment popover.
The reason, you are having this issue is the mis-matched stimulator size. For example, the apple standard for iPhone 6 is a frame of 400 x 600 pt, meaning 2:3. So,
First, turn on the auto Layout and size classes. Then change "wAny hAny" to "compact hAny".
If, you change the Stimulated Size now, you will se the width as 400 and height as 600.
After this you need to add the UIButton at the centre and even without adding any constrain, you will see your button to be centered in the frame.

How to handle iPhone 4s, 5 and 6 screen size, and make all the contents look the same across al the devices

I am using Xcode 6.1 and in one of the views I have, contains many labels. However, these labels look different in each different devices. Although I have add constraints to all the labels, but still facing the same issue.
How can I get the same view layout across all devices?
How can I make the width and height for each label is flexible, depends on the screen size?
Do I need to design different xib for each device?
The following images showing the result am getting, with auto resize enabled, and constants been added.
on iPhone 6
on iPhone 5
on iPhone iOS 7.1
We have provided with a simple solution . Using Aspect ratio constraint we can get a relative look in all device screens.
I had the same issue and I found this >>
http://mathewsanders.com/designing-adaptive-layouts-for-iphone-6-plus/
So when designing for lowest to highest screen resolution we can't set a specific width and height for all controls as it may look weird. So using this constraint we can have a relative look for all screens.
If you don't want to use constraints or you want to keep the same width & height , you can add scrollview to hold all controls. For that you can set a resizing frame for scrollview with fixed content size .
To solve this...
1) Go to xib file and uncheck the Auto layout
2) Then set the autosizing manually in the following manner for each and every subview.
Use self.view.bounds.size.width to get the width of screen, then use relative value to initialize all labels.
One soultion is that you can Design a story board for each screen size like one for iphone 5, one for iphone 6 and one for iphone 6+ and in your AppDelegate method "didFinishLaunchingWithOptions" you can get the screen size and popup corresponding storyboard
in this wasy you can set different constraints according to different screen size

Xcode UIToolbar Autoresize

I have an iPhone application that runs perfectly on the iPhone 4s but when run on an iPhone 5 some parts of the screen doesn't autoresize. The thing that isn't resizing is the UIToolbar. My application always runs in Landscape mode, so the toolbar is at the bottom of the screen and these are the dimensions of the toolbar:
X: 0 Y: 256
Width: 480 Height: 44
I've tried adjusting the auto-resizing tool to almost every combination and nothing works. I can get the toolbar to spread across the screen, but the 3 buttons stay the same size. Each button is of width = 148. Is there an easier way to have the entire application just automatically resize correctly? For some reason my iPhone app worked great on the iPad when you used the "x2" resize view. Thoughts?
so the OP uses stock items and they don't resize then!
I would add flexible spacer items in between each button -- they resize
To solve my problem I just used IBOutlets that connected to each barbutton and if the screen size was for the iPhone 5, the width would adjust accordingly for each screen, if regular screen (iPhone 4s and below); then stay the same. Not a very efficient way but it worked for me.
Take a look at the images and select the autosizing red lines, if you select the bottom line it will keep on the bottom always, and if you select top and bottom it will stay at the same place. So verify your UIToolBar autosizing properties.
EDIT
EDIT2
Check out this answer How to detect iPhone 5 if you can't autosize, one quick solution that occurs mi if to change the frame in the code. Check if the device is a iPhone 5 or 4S to give a specific frame so you can adjut the width.

Resources