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

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.

Related

Swift: Programmatically change size of a button in runtime

I use two buttons in a camera app to track the zoom of said camera. My goal is to reduce the height & width of 1x if 2x is selected and vice-versa but it doesn't seem to work.
I tried button.frame.size.height = 8 button.frame.size.width = 8 and the height & width always remain the same. I even tried 500 to see if it would make a difference and it didn't.
I also tried button.size(CGFloat) to no avail...
I should probably mention that those buttons are nested in a StackView and the app is built on Storyboards. Also, the app is designed to be used on iOS 13+.
Thanks in advance!
UIStackView is using Auto Layout for arranging its subviews. Therefore, direct modifications of the button's frame will have no effect because Auto Layout will anyway recalculate all frames according to the active constraints on the next layout pass.
Since you're using a storyboard, here's how to solve it:
Open the storyboard, select your button and create a width and a height constraint for it.
Create IBOutlets for each of these constraints in your view controller. For the sake of the example let's say you name it buttonWidthConstraint and buttonHeightConstraint.
Whenever you want to change the size of the button, do this:
buttonWidthConstraint.constant = 8
buttonHeightConstraint.constant = 8

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.

Resize UIButtons from swift once constraints are set

I was looking at this youtube video https://www.youtube.com/watch?v=vzyd33Pv9kg regarding How Priority Constrains works.
I want to do the same but instead of label I am using custom buttons with images. As you can see in the video, the author hardcoded the size of the label to 100wx100h. However regardless of the what iphone you have, the labels always be the same size. I want to accomplish the same thing, but instead I want the size of buttons (or in his case labels) be larger on iphone 5, and even larger in iphone 6 and so on.
My question is how can I accomplish this?
Also is it possible design this with let say 100X100 in 3.5-inch iphone then change the size dynamically change in the code depends what iphone is used?
if iphone4s then use 100X100 size buttons
else if iphone 5 then use 110X110 size buttons and so on?
Yes you can. Just connect your relevant constraints(width and height constraints in your case) to your view controller like usual elements. Than you can change constant property of your NSLayoutConstraints (Probably in your viewDidLoad method).
self.buttonWidthConstraint.constant = 110.;
self.buttonHeightConstraint.constant = 110.;
You also may want to change size classes to provide different constrains for each orientation.
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/AboutAdaptiveSizeDesign.html

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

Float layout in interface builder in iOS?

I want add one UIView – which has various number of UILabel objects – to a UIViewController.
How is it possible to set up constrains via auto-layout so, that if rotation occurs, and view width decrease, than height should increase, and labels should fit in like below, as CSS float do it.
Is it possible with interface builder and not programmatically?
UPDATE
Meantime I experimented iOS8 new interface builder feature called Size Classes (WWDC 2014 What's New in Interface Builder from 29:59), as matt has recommended down. But as I see, it supports something different, that I am looking for. Size Classes aim is to put different xibs (iPhone, iPad) into one xib, to have one xib instead of more.
In my case it does not help. What I have done to set up different auto-layout constrains for these two size classes:
But when I was running the application in iPad 4 simulator - what is the main target for my app - it used always the regular width / any height size class, and never the any width / any height, what my original expectation would have.
I do not know what "in a floating way" means. But in Xcode 6 you can easily do what you're describing, changing the constraints in an iPhone app so that the fourth label drops down to the next line, using the new conditional constraints (size classes) feature. With this feature, you can have one set of constraints for one set of size classes (e.g. iPhone in landscape) and another for a different set of size classes (e.g. iPhone in portrait).
Prior to Xcode 6, the answer would be no, you can't do that using IB alone. Code of some sort, whether a collection view or your own code responding to rotation/layout, would have to change the constraints.
With UICollectionView you can set up layout like CSS float.

Resources