I've added a uiview on a view controller(VC), trying to set it's width is equals to VC's view's width.
What I set and expected:
And what i got on iphone 6 plus:
I've tried to change the constants of leading space and trailing space and no help.
Is there any special for iphone 6+ autolayout?
The view setting here is "Any width x Regular height"
-------Added Dec 2016
To people who is confused by this case like me:
There is a layout margin property in UIView, which is an UIEdgeInsets(8,8,8,8). It is something like css padding.
When the edge of your view is close to the edge of the superview and the preservesSuperviewLayoutMargins property is true, the actual layout margins may be increased to prevent content from overlapping the superview’s margins.
The default margins are eight points on each side.
If the view is a view controller’s root view, the system sets and
manages the margins. The top and bottom margins are set to zero
points. The side margins vary depending on the current size class, but
can be either 16 or 20 points. You cannot change these margins.
Try to remove the check Constrain to margins when adding constraints. Here take a look:
Constrain to margins is checked and its result
Now, after removing the constraints and re-adding the constraints with Constrain to margins unchecked, here is the result:
Sharon's answer works wonderfully, but you don't have to delete the existing constraints to solve the problem. You may edit existing constraints removing the extraneous margin:
Editing existing constraints to remove the extraneous margin will not work. You have to delete the existing constraints and add new ones keeping constraints to margin unselected.
I faced same issue for my "Done" button. The issue was my View (Done Button) was in base View hierarchy so i bring view back in root view (view controller's view). and set constraints accordingly.
My case set constraints Pin-> Uncheck Constraints to Margin -> Bottom,Leading,Trailing (Constraints value=0)
Make sure view hierarchy should not be following way.(Subview might be but cut view must not).
I had this bug in the iPhone 6 plus simulator. But in the 6 plus device this bug didn't happen. This could be just a bug with 6 plus simulators.
Related
I'm trying to add some constraints to my view, but everytime I switch the device, the view goes crazy. How can I fix this?
How I want it to look and how it looks like on the iPhone 8 Plus:
How it looks like when I switch to iPhone SE:
I added these constraints:
0 top
0 left
0 right
686 bottom
If your bottom constraint is larger than the height of the device, naturally, it will be out of bounds.
Moreover, your constraints will be ambiguous if the screen's height is smaller than 686.
You'll need to have a height constraint with a constant of 50 to set up on the view rather than a bottom constraint.
It's not preferable to have such a huge value for any constraint. So when changing devices, the height of screens may change and your constraints will break.
The preferred way will be to create a Height constraint and set it to 686 and don't set the bottom constraint as it's not needed anymore. But the problem can arise here if the screen size is smaller, as the views on the bottom will not be visible. To overcome this you should make use of a scrollview and put this content view inside scroll view.
you can set the Height constraint of View instead of bottom constraint
or
you can use the Size classes
My table view on iPhone 7 looks like this:
I added new constraints to my table view: top, bottom, left, right 0;
and I checked horizontally in the container, too.
But on my phone (iPhone 7), like the picture shows, my table view doesn't have a vertical symmetry. Is that normal?
I also tested in a tabbed navigation controller, but the results were the same.
I didn't do anything by code. I just made a table view and added contraints...
What is the problem?
Table view separators have a 15 pixel left inset by default. If you want to change that behaviour, set Separator Insets to Custom inside the Attributes inspector and provide 0 for both values.
Moreover, your leading and trailing constraints seem to be relative to the superview's margin. Uncheck Relative to margin under the constraint's Size inspector settings.
I had inserted a ScrollView into UIViewController and dragged ScrollView to fill the space between the navigation bar and the RAM label below:
But when I run the app, the ScrollView does not fill the space:
Please help me! Thank you very much.
P/S: Sorry for my english is bad.
The scroll view is not covering up the whole thing because you are running the app on a much bigger phone. The simplest solution is to run the app on iPhone 5.
However, if you want to solve the problem on all sizes of iOS device, you need to add constraints.
Constraints are things that tells a view when and how much it should resize and where it should be positioned.
To add a constraint, just select the view you wish to add a constraint to and go to the bottom right corner. You will see 4 buttons:
The leftmost button is used to embedding views in stack views. This is a feature of iOS 9. If your deployment target is lower, just ignore it.
The second button to the left is for adding constraints related to alignment - where the edges of the views are, what its baseline is and where it is positioned in the X and Y axes:
The third button to the left is used to add constraint related to margins, width, height and how the width and height should change when it is asked to resize (keep the aspect ratio, for example):
The rightmost button is used to let Xcode decide what constraints you should add. And I think most of the times its choices are okay. Sometimes though, you still need to do some tweaking before it works.
"So... what constraints should I add?" you asked.
Well, I think I should teach you how to think when you want to add a constraint. This way, you can figure it out yourself in the future.
You should first let Xcode guess what constraints you want. Just click the rightmost button and click "Reset to Suggested Constraints". This can save a lot of work if Xcode can get it right. So remember to always do this first.
Then, run your app on various devices and see if the view's position, size, and alignment are as you expected. If it is not, you might have to add and/or remove some constraints.
For example, if you found that your view is always the same size on different devices, (that could be bad because it means that some content my go out of view on smaller devices) it's probably because Xcode added a width and/or height constraint to the view. You should delete that so that the view's width and/or height is not fixed.
You can find your view's constraints in the view hierarchy:
Just select the constraint and press delete.
Uncheck Adjust subview option and add
scrollview.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
You need to add constraints for your scrollView. Set the leading and trailing constraints to 0. Pin the height of your scrollView and also don't forget to set the top layout constraint. You can either pin the height or add bottom layout constraint to your page control.
Constraints are very important and its even more important to set it correctly. Check the Apple Documentation - Working with constraints in IB
Uncheck constrain to margins and add 0 every one of the four limits of spacing to nearest neighbour.
My guess (from the little information we have) is that you are creating a constraint from your scrollview to the top of your view with a value equal to the height of the navigation bar. Set the value of this constraint to 0.
Just set the 4 constraints to 0 to the area you need and then uncheck the "Content Layout Guides" checkbox in the constraints tab here. It will automatically adjust to the area you have specified.
The checkbox to uncheck
I have:
application, that should work in landscape and portrait mode.
view with full-size scroll view on top.
some view inside scroll view with fixed width and height. (with added H and W constraints)
set to view inside scroll view as horizontal centered in container. (added according constraint)
I have warning in interface builder "Has ambiguous scrollable content width".
The only way to fix this problem, that I know - is set trailing and leading constraints.
But for different iPhones (5.5", 4.7", 4") I need to set different trailing and leading constraints.
How can I eliminate this warning and still have centered horizontally view with fixed W and H for all iPhone sizes?
I create Github repo to illustrate this problem: ScrollViewAmbigous
This is not duplicate of UIScrollView Scrollable Content Size Ambiguity
, but it similar (and not answered although), but this question especially related to different sizes of iPhones.
In the morning with a fresh cup of coffee I figured out workaround for this issue!
So, here is the initial state for simplest case:
scrollView with 0 constraints to all edges
Button centered Horizontal and Vertical with fixed Width and Height
And, of course Has ambiguous scrollable content width and Has ambiguous scrollable content height annoying warnings.
All, that we have to do is:
Add 2 additional constraints, for example "0" for trailing and/or bottom space for our view (in my case - UIButton)
Important: you have to add trailing and/or bottom constraints. Not "leading and top" - it's not works!
You can check it in my example project, that demonstrating how to fix this issue: ScrollViewAmbigous
P.S.
I don't know why it works and how Xcode detect which constraint is more prioritised (because I'm not set priority for these constraints explicity), but I'll be thankful if someone explain, why it works in comments below.
Problem :
The warning is telling us that the the content size of the
scrollview depends on its subviews.
while your subview don't have any strict rule of position and size (no fixed constraints) it will confuse nib generated scrollview
content size.
Solution :
Make only one subview as a 'contentView' for scrollview.
Add strict (fixed) constraints to that 'contentView'.
Best practice : equal width and height to its scroll view.
Add all other subviews and constraints to your 'contentView'.
There seems to be a lot of confusion on this issue. My take is that a UIScrollView must have TWO trailing space constraints, an 'inner' one connecting it to one of it's subviews (so it can know its content width), and another 'outer' one connecting it to a sibling or superview so it knows its frame width.
Same principle applies for height, i.e. two bottom space constraints.
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.