Should I use autolayout in my new Xcode project? - ios

Auto layout makes things easy but still confuses me a lot when it comes to landscape orientation. I have to keep both landscape as well as portrait screen. My application will be for iPhones only i.e from iPhone 5 to iPhone 6s+.
What size class I need to use? everything seems so confusing. Every constraints conflict with each other. I have no problem in understanding leading, top, bottom, trail space. But what if device is rotated? How do I manage things? Please help!

I don't think you'll need size classes unless you're sure that there's going to be a significant GUI variations between different screen sizes or rotations.
In terms of rotation, the constraints don't change their orientation when the device is rotated. They still stand for what they stood for before.
For size classes, it depends on the app's design. Most likely, if you've setup your constraints correctly, you'll be fine with just a single size class for an entire family of devices.
If you choose to, for example, have significant changes in a tablet-focused variation, you can make those in code depending on the device type or screen size, or use a size class. Only use a size class to have variations in layout between various screen sizes that cannot be handled with constraints alone.

This site is a useful visual reference for size classes.
If you're making an iPhone-only app, what you basically need to know is that every iPhone uses regular height in portrait, and compact height in landscape. So focus on those two size classes.
Set the storyboard size class (in the bar under the canvas) to wAny hRegular. Then select your view controller and (in the Attributes inspector) set the size to iPhone 4-inch (or other device of your choice) and set the orientation to portrait. (These view controller settings don't interact with the size class but make it easier to see how things will look at runtime.) Now set up your portrait layout constraints.
Then set the size class to wAny hCompact. Change the view controller's orientation to Landscape. Now set up your landscape layout constraints.

Related

Steps to convert iPad app to universal, ideally with just one storyboard?

I've had several attempts to try and convert my iPad app to make it universal.
I'm only supporting landscape on iPhone / iPad.
My existing iPad setup uses a storyboard without autolayout, which works fine.
My app had lots of images and about 10 screen each with a minimum of 6 views on each, however some have 20 or 30.
I decided a couple of weeks ago that it would a far greater nightmare with all the images, so I've created paint code classes for all my images.
When I first turned on sizing classes / autolayout, Xcode asked if it should enable suggested layouts, this seemed to make sense. However it's made a real mess of the any / any layout.
Without adjusting the sizing classes setting any / any, I tried to install constraints for differing sizing classes. I want to make views bigger to fill the space, so I installed constraints with equal width (with aspect ratio) to subview with a multiplier so I would get proportional sized views. However this means I have about three sets of constraints per view. Which is a nightmare on my simple screen.
I then thought it might be better in my case to leave my iPad storyboard and have an iPhone storyboard. But my iPhone 6+ layout won't look very good. Turning on sizing classes made little sense with multiple storyboards.
I'm now thinking I could click the sizing class to regular / regular and compact / compact and have different views for each?
However any / any, which I can't get any sort of layout to look good, will still need views?
I'm also wondering if should let Xcode setup autolayout when I first enable it? As I say any / any looks bad.
I was thinking I could just plop my views in a vague position with any / any, apply suggested constraints, then click / flip to compact / compact and regular / regular and move things around.
I guess I'd also need to do that for compact / regular for 6+ too.
I'm just not sure what steps to take, I can't even think of workable plan b.
I've watched a million videos and feel I understand but I can't put it into practice.
HELP
My Suggest steps would be:
When we use autolayout and size classes, We always take start from wAny hAny
Take your simple Screen First Let say your login screen on ipad LandScape without autolayout will look like this:
Now you enable autolayout and size classes. Change Scene size to inferred (600, 600).
Now your view look terrible:
First Adjust it by just moving the views to center and correct places like this.
Now we are all set for applying autolayout constraints.
First give constraints to parentview(grey view). Unless parent view has correct constraints, childs view cannot be given correct constraints. So always make sure parent view have correct constraints.
Now we select the grey view and apply constriants like this:
Center vertically and horizontally in container.
Add width and Height Constraints. You got options. Give fixed width and height and modify it for different classes. like this:
Or you can add proportional constraints for width or height.(Better)
Now start with childs. Give them leading, trailing, top and height.
Height of label is 50 for wR hAny and 40 for wR hCompact
Final Results:
IPAD LANDSCAPE:
IPHONE LANDSCAPE:
Autolayout and size class are great and easy when someone get used to it.
HOPE IT HELP.
You have 2 options
1) Create separate layout for iPhone and iPad. In this options you might have to compromise targeted customization for iPhone6+. This option will consume less time, and if you have a time constraint or you think that you are not convenient working with autolayouts then go with this option.
2) Using any/any size class and add constraints to make you interface alright for iPad. After that only modify/add those constraints for iPhone specific size classes which needs to be adjusted. Once you have layout set with any/any for one of the device either iPhone or iPad, you will be surprised to know how much less constraints you will have to modify for specific size classes. This option requires good understanding of autolayouts or even if you are not convenient working with autolayout but willing to try and have no problem with time constraint then you should definitely go with this option.

Portrait Layout is shown properly only after changing the orientation to Landscape and back to Portrait

I have a simple layout with multiple views and constraints to position and size them as I need. When I deploy the app to a simulator or device, the X, Y and Widths of all the views have the proper sizes. The Height on the other hand is always 0. All the views have proportional heights according to the super view.
Now, if I flip the orientation to Landscape and then back to portrait, everything shows up properly (the heights are no longer 0).
I've used the same logic with proportional heights previously, and I started seeing this problem with newly created view controllers after updating to Xcode 7.
EDIT You can get the .xib from: http://1drv.ms/1M81EUQ
you have to Set different view update method call regarding the orientation changed if you use auto mask resizing or used autolayout to fix this .
Your .xib was configured with Size Classes enabled, but your views and constraints were all configured to only be applied for Compact width, Regular height.
I went through and changed the views and constraints to apply to Any width, Any height, and this resolved the issue with landscape layouts.
Download the modified .xib file here
I recommend using Xcode 7's Preview Assistant View when working with Autolayout, as it will let you see multiple devices, in multiple orientations at the same time. This really helped in this case with showing that the layout was fine in portrait, but not in landscape.
This is what I saw in your .xib as you provided
And with the changes in my .xib
How could this have happened? One way is that you may have accidentally set the storyboard editor to a specific size class. If you see a blue border at the bottom of the editor, anything you add will be configured to be only installed for that specific size class. You can click on the size class shown to change it out to w Any h Any to return to the default mode.

Change base values to Any Height Any Width on Xcode?

So I've spent a lot of time creating an app and I noticed that I've been building it on a compact width and any height base values. Can I keep my changes and set it to any width and any height? Because now when I change it to that all my changes have disappeared. (my changes are EVERYTHING, I started making the app using compact width and any height)
Or will I need to switch to any width any height and restart my entire app? :(
please help
A size class doesn’t necessarily map to one device in one orientation. For example, an iPad can have a view with an iPhone style layout (i.e. compact) when presented in a smaller space and an iPad style layout (i.e. regular) when the space is larger.
This means you can have a unified storyboard when building a universal application.
If you think there's anything wrong with it you can disable some constraints. These constraints won't apply to your current layout but still work for others. You can add some new constraints and the new constraints will work at the current size class.

Xcode 6 Problems with device sizes/constraints

I am building a single view application with 12 different view controllers. I built all of the view controllers in "any width" and "any height" and added all constraints for each page and also first ran the simulator with an iPhone 6. However when I switch to other devices to run in the simulator, smaller and larger, the images I have on the view controllers become very distorted and the font sizes become disproportioned with the rest of the screen, despite adding in all necessary constraints. Do I have to redesign each view controller to fit each size (for example regular width and regular height for iPad or compact width and regular height for iPhones in portrait, etc.)? Or is there another way to maybe get all of the images/ font sizes into proportion with each screen size, again small and large? Thanks
Constraints are fine in this case, you shouldn't have to redesign every view for each device size. For your images, try changing them from size to fit to aspect fit or aspect fill in the right-hand pane in Storyboard. This will keep their aspect ratios intact. I suggest starting out with using reset to suggested constraints on all views that you can before you fully get the hang of constraints. This will generally work most of the time.

Any-Any size class ignored for ios7

I'm developing for both iOS7 and 8 iPhone.
Since iOS7 doesn't support compact-height size classes, I thought the consensus way to go about this was to use the any-any size class for landscape iPhone layouts,
and use portrait compact-width regular-height (cWrH) size class for portrait iPhone layouts.
I set a constraint on a view in any-any, then modify a constraint constant in cWrH to be specific to that size class. However, iOS7 device as well as Xcode preview ignores the any-any constraint, and uses the cWrH constraint for both portrait and landscape orientations.
I have no clue why this is happening. All I want to do is set different constraints depending on orientation.
This is easily replicated using latest Xcode 6.0.1.
Semi-related: How can Xcode 6 adaptive UIs be backwards-compatible with iOS 7 and iOS 6?
Screenshots below:
As per your question what you did is you have given leading,Top,height and width constraints in Any-Any Size Class.
So now its position will be fixed for any devices in landscape and portrait.
Now when you changes size class to compact width and make changes in top constraints it will affect both portrait and landscape.
What you need to do using size class with Any-Any to place your controls in View controller and set those constraints which you would like to keep in both portrait and landscape.
e.g.: If you want to keep height and width fixed give those constraints in Any-Any Size Class.
Setting constraints in Any-Any Size Class will keep those constraints similar for both portrait and landscape.
1.) Setting leading and top constraints in portrait you can use width- Compact, height-Regular.
2.) Setting leading and top constraints in landscape you can use width- Any,height-Compact.
Visit link below it has the great explanation and your most of issues related to auto layout using size class will be solved:
http://mathewsanders.com/designing-adaptive-layouts-for-iphone-6-plus/
Hope this solves you problem.

Resources