Xcode 8 : Auto Layout Issue - ios

I have few problems with the auto layout. It looks perfect in Xcode for every iPhone models, but when I try on my iPhone 5S, the buttons don't look same as with my main storyboard. It works perfectly only for iPhone 7 and 7 plus sizes.
I used "Reset to suggested constraints" function. The Tic tac toe board is imageView and I have 9 buttons on it. These buttons don't look properly for iPhone 5s and 4s sizes. How can I fix it?

For the board you can user a view with black background color and for the buttons, put the buttons inside the board view by giving their height and width according to the height and width of the view. For example, button's height should be equal to (1/3 of view's height- (the spacing between the button)/2) and similarly do this for the width.
For the position of the buttons, position them to horizontal and vertical to board view and change the constraints multiplier accordingly(may be 1/2 for the first button , 1 for the center button and 3/2 for the third button) and similarly do this for the vertical position of the button.
Set the board view constraint to the main view, so now whenever the screen size changes your buttons and view will automatically be adjusted.
If you didn't understand my answer or need more clarity or help, just comment on this tagging me.

Related

Storyboard buttons are off-kilter

XCode 9.3: macOS 10.13.4
I have an swift app where the buttons are off-kilter. I need them at the bottom of the screen and centered. The iOS simulator for iPhone 8 shows the buttons correctly. However, the simulator for the iPhone SE and the actual SE (my iPhone) show the buttons anchored to the right edge of the screen. How can I insure that the buttons are displayed correctly on all iPhones?
First sol
this layout can be done correctly if the center of the middle button is equal to the center of the screen
Second sol
create a UIStackView with these constraints
1- left , right , bottom to screen
2- height say 100
with distribution set to fillEqually , spacing set to say 20
drag the buttons inside it and they will look right in any device
Remove the constraints from the 3 buttons: Calc, Clear, and Help
Align clear to be horizontally centered
Align Calc trailing offset from Clear's leading.
Align Helps Leading offset from Clear's trailing.
Align Clear bottom offset from the bottom margin.
Align Clear, Calc, and Help along to center vertically with one another.
You have two choices: 1) use the legacy struts and springs mode in interface builder to distribute your items as you need them or 2) spend the time learning auto layout, and use it instead. There isn't any easy answer because of all the different screen sizes, and you haven't even considered iPad screens here. The best way to pick up auto layout is to view the WWDC videos on the subject. Yes, it will take longer, but your app will respond better to screen changes, and you will also be able to inherently accommodate new screen sizes.
Use a horizontal stack view for buttons, and set spacing between buttons.
Approach 1 - Set trailing space and leading space, pin the view to bottom and set a fixed height for stack view.
Approach 2 -
Set fixed height and width to stack view and center horizontally, and pin the stack view to bottom.
Always try to use stack view, with that you will reduce the number of autolayout issues you run into.
Please add the constraint for stack view and adjust like that:

How to setup constraints in storyboard with autolayout for dynamic position on different device sizes

i have a question about how to setup constraints in a storyboard with autolayout if I want to guarantee multiscreen support so that all UI-controls are accessible and the UI is not messed up on smaller screen-size devices (IPhone 4s or IPhone SE) when I designing on a bigger screen-size device (IPhone 6). I have a design which is build up on a IPhone 6 (in a sketch-file) which I want to transfer to my storyboard. So in general I want to setup all the view stuff in a storyboard and not via code.
Here are my constraints. The button to the bottom has fixed height of 48 and a leadingMargin to the right and left and a vertical distance of 100 to the bottom. The top button has the equal height to the bottom button and a distance of 28 to the bottom button.
Here is the designed View with the mentioned buttons on an IPhone 6. This is the default size where I want to setup the storyboard cause of the sketch file.
Here is the View on an IPhone 4s
As you can see the buttons are to close to the middle cause of the fixed vertical distance and the fixed height. I mean this is obviously cause of the fixed values. So I made some research about percentage position like mentioned here but is this the correct way? Also other ways looks so complicated to me. How can I prevent fixed values like the height and the vertical distance? Is there a way to set the fixed height of 48 for the IPhone 6 and then scale the button down (for IPhone SE) in regards to the screen size (the same for the bottom vertical distance and other fixed values)?
Can someone give me any advice how I can proceed here?
It is a bit complicated I try to explain it as much as I can.
Note: Auto-layout is just like constructing a building every thing step by step.
Select top button and add these constraints (Leading:8,Trailing:8, And select Aspect Ratio checkbox) + And also add "Horizontal center and Vertical center constraints" select the vertical center constraint and change it's multiplier to 1.5 or increase or decrease it's value accordingly.
There should no error after this only warnings if any.
Select second button and add Top constraint to 8. Select both buttons and add constraints which are highlighted in screen short.
Run and check on different screens. Hope it help.
bottomButton.bottom = superview.bottom * 0.9
OR
Change the bottom constraint by code
bottomConstraint.constant = xx
self.view.layoutIfNeeded()

Need to fix autolayout issues with only UIButton's and UILabels

Currently i am trying to use AutoLayout on my iOS app in xcode 6. The storyboard I am using consists of only UIButton and UILabel elements, which i would like to resize and fit based on the device. Everything works on the iphone 6 Plus and 6 but when I get to the 5S or 5 things get weird. I can't use size classes necessarily due to both 5s and 6/6P using compact width regular height. IS there a way to fix this?
all images can be found here
Yes! With autolayout there's always a way.
From those pictures it looks like you may have a vertical constraint on the "play" button that is attached to the top of the view. Try putting that constraint on the "How Long" label. That way the button position will be relative to the title instead of the whole screen.
Check to see that you have the following constraints :
Center all buttons and labels horizontally in the container (note,
this is 3 different constraints you need to set). You can select
all 3 at once and from the Align menu center them horizontally in container.
Constrain How Long to Top Layout Guide (Top Space to Top Layout Guide)
Constrain Play to How Long (pin vertical spacing)
Constrain Leaderboard to Play (pin vertical spacing)
Depending on how you want Leaderboard to float you may want to pin it to the Bottom Layout Guide instead of to play. The buttons and labels will then move according to your constraints.

Auto layout: Xcode 6: Centering UI elements

I'm using Interface Builder in Xcode 6 to make an app and am having trouble getting the text fields and button to centre on the screen for different size screens.
I thought it was a matter of selecting horizontal and vertical centering in container but it doesn't seem to be that when I try it in auto layout. Actually I've tinkered around a bit and I still haven't got it.
I just want to be able to see all of my button and text fields for any size iPhone screen and right now simulator is only showing part of these UI elements like this:
I also want to do this in storyboard and not in code as I'm not at the level of doing this in code yet.
Step 1: Make sure your size class covers all the iPhone screen at least in portrait view. So, change the size class to "wCompact hRegular".
Step 2: After setting the size class properly, add the UITextFields and UIButton to your storyboard. To me, it looks something like-
Step 3:
Before, you start adding constraints, you need to remember two things-
a. Your element(UITextField, UIButton, UIView or any component) needs to know its starting position unambiguously, and
b. Your element needs to know its size meaning, its height and width.
In this case, as you want to centre your elements, I am just assuming that it needs to be centred starting from 10 scale from the left edge and should end 10 scales away from the right edge of your iPhone screen. Now, that means, it's width will be different based on the screen size, but its height will be same.
So, I just add the constrains following way for the 1st text box-
Notice, in the size inspector, I set the text box's starting point, x and width in a way that it is 10 pt away from left edge and 10 pt away from the right edge. Don't worry, it's just simple math.
For the 2nd textField, I add the constrain, the same way-
Lastly, for the button, the constrains are following-
Now, you are good to go. Everything is centered.
By using your size class selector in the bottom of the storyboard window, set you sizes as any width and any height and then follow the below auto layout constrains. It will work for you.
First select the view you want to set the auto layout, and then select the pin option from the right bottom corner of your storyboard and then add the constrains as shown in above picture and click button Add 4 constrains
Repeat the process for all views and set the constrains as Fix the top, bottom, left and right constrains of all views except the last button that should be fix from top,left,right and fixed height.
You need to make use of the size class selector in the bottom of the storyboard window.
So for an iPhone 6 or 6 Plus in portrait you would choose a compact width and regular height like this:
And then you would do whatever auto layout stuff for the given device there

xCode 6 constrains for ios 7 and 8

I'm struggling to get my constraints working for both ios 7 and 8 in xcode 6. My app is just a webview that should take up the whole screen and not appear under the status bar.
My problem is that it only displays correctly in either 7 or 8, but never both at once. Depending on how I adjust my horizontal constraints it's either too big for the ios7 screen and fits the 8, or too small on 8 and fits 7. Here's the issue and constrains I have set up:
*Edit: I think maybe my superview is positioned incorrectly, since my constraints are relative to the super view. I just can't figure out what would be wrong with it.
ios7 fits
too small on ios8 (green background is the parent UIView)
20point top constraint to move the view below the status bar
0 side constraint so it resizes on orientation change
0 side constraint so it resizes on orientation change
0 bottom constraint so the view isn't pushed below the bottom of the screen
I think you need to pin this webView like this
So apparently there were constraints on the parent view that I had the webview in. They did not show up in the left nav like the webview constraints did so I was not aware they were there. In the right inspector panel click on the ruler icon with the view selected and you'll see the constrains. After removing those it was just a matter of tweaking the constraints on the webview to align things properly.

Resources