Unable to get the constraints right in xcode storyboard - ios

I was playing around with iOS 8 auto layout and constraints and ran into this simple issue, and wondering if someone can help me.
All I want to is to create view (uiview) and have a label at the bottom center.
So I created a new project, Main story board is set to wAny hAny.
Dragged a view into it
x: 16, y:20
Width 275, Height:560
Color : Green
Added the following constraints
Pin top space to superview
pin leading space to superview
Pin hight
Pin width
Added a label and centered it to the bottom of the view
on the label, pinned leading space to superview
pinned bottom space to superview
Currently not seeing any constraints error
using iPhone 6 in the simulator
in portrait mode, I can see the full view and the label at the bottom
Now rotated to the left which is now the screen is in landscape mode. but the view is still on the left side of the screen vertically and 1/2 of it is cut.
So my question is how can I fix this issue so the view also rotate or I can the whole view with the label?

Generally you don't want to be pinning to one side and not the other because as the view size changes (such as with rotation) only one side of the view will get pulled, and that's exactly what you're seeing.
So, either you should not pin and instead centre horizontally in the superview (and allow the intrinsic content size of the label to set the size) or you should pin both leading and trailing (so the label will always be resized to full width).

Related

Button on bottom of screen: iPhone X vs regular screen

I'm trying to optimize an app for the iPhone X screen. Currently it shows black or white bars on most screens. As example I will use the PaymentMethodsView.
The PaymentMethodsView is a custom UIView containing 1 or 2 buttons in a horizontal StackView, the superView has a gray background. This StackView is currently constrained with 16px to the superview (all 4 sides). Currently it looks like this on the iPhone X (and on regular iPhones the view is just on the bottom of the screen:
What I want to achieve here is that the gray area extends to the bottom of the screen, while the yellow sits where now the bottom of the entire view is. (So 16px lower)
I can get this to work by enabling Use Safe Area Layout Guide for this ViewController, constraining the PaymentMethodsView in the viewController to 0 to safe area on the sides, and 0 to superview on bottom. Then in the PaymentMethodsView itself, I change all the StackView's constraints to safe area instead of superview, and change the bottom constraint to 0 instead of 16.
This works fine, except on regular iPhones the bottom 16px (under the StackView) disappears and it puts the yellow button to the bottom of the screen.
How would I go about fixing this?
On iPhone X: The gray area to the bottom of the screen, the button 0px to the bottom safe area
On regular iPhones: The gray area to the bottom of the screen, 16px between the yellow button and the bottom of the screen.
Thanks!
You can do this by adding TWO bottom constraints to your stack view...
First, constrain the grayView Bottom at Zero to the bottom of the view (its superview), not to the safe-area. Then
add one constraint from the stackView Bottom to the bottom of the grayView (its superview) at >= 16
and a second constraint from the stackView Bottom to the bottom of the safe-area at 0 with Priority: 999
This says: keep the bottom of the stackView at least 16-pts from the bottom of its superview (the gray view)
and
The 999 says put it at Zero-pts from the bottom of the safe-area if possible
The result on an iPhone 7:
and on an iPhone XS
Below code worked for me give it a try,
Click on you view controller
Select Identify and Type from right panel
Under Interface Builder Document enable Use Safe Area Layout
Now Wherever you want to place any view to bottom just give constraints corresponding to view not safe area

How to Re-Size Label to fit any screen size

Ok, here's what I'm trying to do:
I would like to create a scroll view to scroll vertically, in case my text is longer than the screen size (think iPhone 4 vs. iPhone 6).
I've added a scroll view and placed a view within that. I embedded by label for the text within the view.
However, the scroll view currently will scroll only horizontally.
Is there a way to constrain the label width to be the width of the screen? How do I do this?
You can constrain the width to contentview to be equal to scrollview width. And pin the four sides of label to the content view.
Also you will need to pin all sides of content view to scrollview, if you haven't already did that.
If you push the constraint button on the bottom of your storyboard pin it to both the left and right side. Uncheck constraint to margins and pin it both 0. :)

Xcode 7 Vertical Scroll View Issues

I'm having trouble creating a vertical only scrollview in Xcode 7.3. I've followed a couple of tutorials on SO and other places but for some reason it seems that my topmost view's width extends past the screen or is shifted to the left. The scroll view is restricted to vertical scrolling only, which is my intent but the content appears partially off the screen.
I try to horizontally center buttons and pin to the top and bottom of the content view. On the simulator they appear half off the screen to the left but they appear fine in the storyboard with no constraint issues. I even preview the view 3.5, 4, and 4.7 inch screens in the assistant editor and it looks as I expect it. But when I run it on the simulator it's messed up. Also the background of the Content View is supposed to be white (for testing purposes) but it's showing the gray color of the top-most view.
This is what I do along with pictures of my constraints
Add Scroll View and pin to the super views top, bottom, left, right (0,0,0,0)
Add Content View and pin to Scroll View top, bottom, left, right (0,0,0,0) and also add a constant height of 1000.
I ctrl+drag from Content View to View (parent of Scroll View) and set equal width constraint.
Place 2 buttons inside Content View pin 1 to the top and the other to the bottom. Both are horizontally centered as well.
This is what it looks like on the simulator.
Simulator-Actual
And this is what I its supposed to look like on the iPhone screens.
Storyboard-Expected
Give it horizontal centre constraint and than check
I think you have set all constrain correctly . But still check this once again.
Step 1 :- Remove tick Adjust scrollview insets
Step 2 :- As you said Give Leading , Trialing , Top , Bottom = 0 .
Step 3 :- same as step , set Leading , Trialing , Top , Bottom = 0 from superview
Warning :- You need to check both the constrain inspector because sometimes it takes Leading space from -20.
Step 4 :- Set constrain to 1 st button Top , Leading , Trialing
Step 5 :- Set Leading , Trialing , Vertical space to button 1 and Bottom (bottom from UIView)
If you give vertical spacing to button1 and give bottom to view then you dont need to set contentsize or else , it will scroll Automatically.
I hope this will be helpful for you.

AutoLayout UITableViewCell

I am working with autoLayout for UITableViewCell
So here is my xib and constraints
Here I set constraints such that tableView has dynamic height
Whenever I run on iphone5, it looks this way correctly which I want
But when I run the same thing on ipad it shows this way
So I am not understanding how to make the ipad version look same as iphone version, Not understanding which constraints I am missing.
When working with autolayout constraints, formulate what you want into sentences.
Example:
I want the yellow view to be pinned to the right.
I want the yellow view to be pinned to the top and bottom.
I want the yellow view to have width of 50.
I want my label to be pinned at the top and bottom.
I want my label to be pinned to the left.
I want my label to be pinned to my yellow view, with 10 pixels between them. (Thus growing in width along with the superview width).
And there you have all your constraints. Now you just have to add them one by one. Top, Bottom, Right to superview and Width constraint with a constant of 50 for the yellow view. Top, Bottom, Left to superview and Right to Yellow view with constant of 10 constraints.
You've pinned your yellow view to the left of the superview, so on bigger screens, it will grow to fulfill that constraint.
It looks like you pinned the left edge of the yellow view to the left edge of the table view cell with a 300pt offset. That means on the iPad, the yellow view is still 300pts offset from the left edge of the screen, and grows to fill the rest of the width available.
What you probably want to do instead is pin the right edge of the yellow view to the right edge of the table view cell with a 0pt offset, then also pin the yellow view's width to its desired size.

iOS view height and auto layout

I have a viewController, which is configured for the iPhone5 screen size. In red rectangle is the view.
To make this view show properly in iPhone4 - I used auto layout. But when I specify constrains such as: leading edge, trailing edge, bottom space and top space (in top space I even made constraint's priority to LOW ) - the view still goes partially down the screen as if my constraints don't work.
BUT if instead of top space constraint I specify view's heigh and delete top space constraint - everything works perfectly.
Does anyone can explain it please? Thank you.
Thats because when you set up the top space constraint it will move the view by the constant you provided. iPhone 4 and iPhone 5 screen height is different but the constant remains the same so obviously it will behave differently. One way to troubleshoot your interfaces is to switching between iPhone4 and iPhone 5 on storyboard device on storyboard (first button from left to right on the bottom right corner of interface builder).
Auto-layout is all about experience in my opinion. I struggled alot with it until i learned. If you want your view to be attached to the bottom of the screen you should set the BOTTOM SPACE to 0 and specify the view's height like you did or adjust constraints for it's subviews so that the height is set dinamically according to the views inside.

Resources