How to create different layout for iPhone and iPad using Autolayout? - swift

I'am creating a UI for both iPhone and iPad using "Vary for Traits". But I'am getting confuse when to choose Width or Height.What happen when I select Width or height or both

It will really help to know what the size class constraints mean. See my https://stackoverflow.com/a/52317547/341994 for a summary.
Okay, so what the checkboxes mean depends on what the current traits are for the storyboard you're looking at.
First set the device type you want to design constraints for, on the left. Now you will see some size classes. If you chose an iPad, the size classes are wR hR. That is width Regular and height Regular.
So now click Vary for Traits. You are about to design some constraints. Which size classes are you about to design? Compare that to what we just said. If width should be Regular click Width. If height should be Regular click Height.
So for example if you chose an iPad on the left (wR hR) and you now click both Width and Height, that means Regular Regular, which means “I am about to design for iPad.”
But if you chose a small iPhone in portrait on the left (wC hR) you might now click just Width, which means Compact Any, because you don’t care about the height constraint; you just want Compact Width which means “I am about to design for a small iPhone in any orientation.”

Related

Basic Autolayout issue, resizing button after setting constraints

Sorry for the super basic question, I'm not understanding why (or how) Autolayout automatically resizes buttons/labels after setting constraints to the safe area/view.
Here is a short video demonstrating my exact problem. I want to keep a 200x100 button centered horizontally near the top of the screen, why does it resize itself after setting constraints?
If anyone could explain/point me to a good guide for getting started with auto layout it would be greatly appreciated!
You need to set constraints for the width and height, not simply set the width and height. Setting constraints will ensure that the width and height do not vary from what you set them as.
Check out a demonstration below of how I set the width of a button equal to 200, the height equal to 100, the offset from the safeArea top to 20 and horizontally centering the button:
You should set the constraints for width and height, not just size in Size Inspector. Make sure you set them as follows:
The two basic rules of autolayout are:
Autolayout and frame/size are opposites. You can only use one of them.
If you choose to use autolayout, you must completely determine both position and size using autolayout.
So, at first in your video you are using frame/size. So the button obeys your size settings.
But then you come along and add some constraints. At that moment, you are using autolayout — and your frame/size settings cease to mean anything.
Well, the rule for something like a button or a label is that unless you explicitly set width and height constraints with the desired Constant values, it will size itself to its contents as an automatic fallback (using its own internal constraints that you can’t see). So that is what it does.
The easiest way to add a width or height constraint is to control-drag internally inside the button, horizontally or vertically. But you still might need to set the Constant back to what you wanted it to be (in the constraint's size inspector).

Auto Layout Swift 4 Xcode 9 For Multiple Devices - Am I doing it Wrong?

I am having severe difficulties finding a clear working world example of auto layout on multiple screen sizes. Most tutorials have basic stuff like a button thats centralised and then after adding constraints in Interface Builder it scales with the device. One button is simple but in my example for the iPhone 4:
There are quite a lot of labels / buttons and some that are not central. The above screen shot is exactly how I want it to look on all devices.
The app is for portrait only but I want to target all devices. I tried slowly adding constraints to items from the bottom up using a multiple of the superview to position and size items and then applying aspect ratio to UI Objects (got as far as the got it button but not the red arrow buttons) but as you can see in the next picture for the iPhone 8 plus... the white space isn't going to perfectly fill like in the iPhone 4 which will leave me with a big gap at the top.
Is there a way I can change just the iPhone 8 plus layout in Interface Builder to build a layout that caters for the extra screen size?
The iPhone 8, SE and X layouts are equally awkward with extra white space.
I tried size classes but that changes it for all phones.
Is there a way to centre what I have done so far?
How do you guys position objects in auto layout that are not centred?
Am I going about it the wrong way by applying X / Y constraints using a multiple of the superview? - same for the width. I'm only specifying widths as a multiple of the superviews width then adding aspect ratio.
I just need a real world working example I can learn the basics from or a system to create for multiple devices that I can follow. I don't want to use a 3rd party library.
I really hope someone can help point me in the right direction!
Krivvenz.

How do I set specific y position, width and height without diving into code?

A designer I'm working with wants to have a UIImageView be at a specific y position, with a specific width/height depending on the iphone size. I've done research and I haven't been able to find anything online that allows me to do so with constraints on the story board. I want to avoid having to use code because throughout the rest of the app there are a lot of similar situations. In the past I've made different storyboards for each phone size to accommodate for the specificity our clients request, but I want to avoid doing this because it feels like bad practice. Is this even possible?
This can be completely handled in the Interface Builder. Follow the steps below.
1) Add your UIImageView to the ViewController
2) Set constraints for Width, Height, Vertically Align and Horizontally Align
3) You can adjust the Y position in the Size Inspector of the view.
4) Now switch to Assistant Editor and choose 'Preview' and add different screen sizes to see the auto layout in action.
All of this will set a static image size of 250x200, horizontally centered, and vertically at 1/4 of the screen height.
Little-known capability of constraints are that you can set view positions, or in your case sizes, by way of ratios against other views.
With just your image view selected, the tie-fighter :) constraints button lets you specify absolute positions & sizes: do that to set your desired y-position. But then select both your image view and its enclosing parent view, then the pop up for that same button now enables the equal height/width buttons. Add those constraints and then edit the constraint to change the 1:1 ratio to whatever ratio you need.
Excuse my lack of screenshots, posting from my phone.

cocos2d-x FIXED_WIDTH isn't working

I have a 700x1280 image and my design resolution is 700x1280.
I am not providing separate assets for each device that I target rather i will use one Resource which contains all my assets.
The problem:
When I set my design resolution I pass a ResolutionPolicy::FIXED_WIDTH meaning from what I understand it will scale the width with no bordering but the height may be bordered. But all I see is a fixed width but the height of the image goes out of the screen i.e it's not making the height fit in the screen like FIXED_WIDTH is supposed to do?
FIXED_WIDTH only promises to keep the width using the full physical width of the screen but doesn't promise that the height will stay the same i.e it may go out of the screen. For portrait mode FIXED_HEIGHT is the way to go because your height on any device is definitely bigger than that of the width so by using FIXED_HEIGHT the width will never go out of screen, it's a bummer but that's what you have to do!

Loading levels from xml and supporting screen resolutions in Ios

I am developing a game that uses levels. The levels are made at a default scene width and height resolution.
The thing i am worried about is when the game is played on IPads iphone 5′s etc, the position of sprites loaded from the level xml files will be out of place due to the screen size.
In my case, could someone tell me the best thing to do in this situation or some advice on the approach i should take?
Also if any has experienced this, please let me know.
Thanks. :)
Generally this has nothing to do with how or where you store the level data.
These are the standard approaches, which one works for you depends on your requirements and desired results:
design each screen resolution individually (error-prone, tedious)
design for one screen resolution, then scale up or down according to screen aspect ratio (can lead to skewing as the screen size scaling for width & height are likely to be different)
design for the smallest screen resolution, then center the contents on the screen (this leaves unused areas either at the top/bottom or left/right sides)
same as above, but zoom content to fill the screen (this will remove the letterboxing, but also partially remove one side's content from the view)
In essence this is the same problem as movies have in trying to fit to screens of varying aspect ratios.
In general this is all a matter of scaling the input (positions) to a desirable output. The easiest approach is to maintain aspect ratio and allow for letterboxing. However Apple may reject letterboxing apps if nothing is done to hide the letterboxes (black areas) since Apple requires apps to support widescreen resolution, and letterboxing does not normally fall into their definition of "supporting widescreen".