Why constraints is always Zero,is Storyboard preventing from edges? - ios

Let me trying to explain my question.I have noted from long time during app designing in Storyboard/Xib. In all cases for eg while dragging some controls into storyboard obviously it shows the blue lines helps to us place controls correctly.But it's seems prevent controls from UIView(main view) layout to place edges on controller.
When we trying to place controls the blue lines prevents us from edges.see below image
But why constraints always zero even we are not placed at edges..it should be approximately 10,10,10..,
So my Qus is
Is Storyboard/Autolayout preventing to place controls at edges?
If yes.Then why all constraints is Zero ?
If No.Then what is the use of corner blue lines(eg:first image)?

They are the content margins of each view which are 8 points away from its actual borders. Please check this answer, which is explained it in details. Also you can place controls in the edges, just unchecking the constrain to margins will do that

Storyboard/Autolayout is not preventing to place controls at edges, you can always place your objects there and apply auto layout by unchecking "Constrain to margin" option in auto layout menu.
It is zero because "Constrain to margin" option is checked. Placed object distance will be calculated from that bound margin line as long as "Constrain to margin" option is checked. Uncheck it and you will get exact values from edges.
The use of this blue line is... if you add any UIScreenEdgePanGestureRecognizer, it will work outside those margin lines. placing an object between the edge and provided blue line may interfere with this kind of gesture recognizers.

Related

How to remove whitespace and extend background to full screen in Xcode?

I'm trying to create a simple game, but the background won't extend to the edges of any of the displays (iPhone and iPad) so there's whitespace at the top and bottom. In landscape orientation there's even more whitespace for some reason. I haven't coded anything yet.
Here's how it looks: https://imgur.com/a/pDupTOD
I have constraints added to all four sides of the Image View and them set to View and 0.
I have also tried unchecking "Use Safe Area Layout Guides" and "Safe Area Relative Margins" neither of which helped.
You have set the constraint to safe area. Instead of pinning your view's edges to safe area, pin them to your superview.
Here is how your constraints may look
Select each one of your constraint(double click on it)
And change the following, i.e. from safe area to superview. Also update the constant and set to 0.
Hope that helps

UICollectionView change flow direction on rotation

Hey, I'd like to obtain what you see in the pictures: in Compact Height mode (landscape iphone) both the red and the blue view have to take all screen vertically and half the screen horizontally. In Compact Width mode (portrait iphone)they have to take all the screen horizontally and half the screen vertically. Space between views should be same size in both modes.
I used to think I have to use size classes and auto-layout constraints, but everything I tried failed miserably.
Maybe I have to use a UICollectionView and change flow direction based on orientation (if that is even possible)?
A collection view is probably overkill, because you don't want scrolling and that's the whole point of a collection view--by the time you do the sizing to stop it you'll have done all the work necessary to set a non-scrolling layout.
This is possible with Size Classes in IB. First, In general you will probably find it helpful to name the views in the Document Outline on the left in IB. You will also want to use this outline rather than try to grab the tiny constraint H-lines.
Set up all the constraints except 1) constraints linking the
OrangeView and BlueView to each other, 2) the constraints linking
the OrangeView to the top and left(leading), and 3) The constraints
linking the BlueView to the bottom and right (trailing).
Change the size class setting at the bottom to w-Compact and
h-Any in the funky box system. Now we're designing for a compact width, so views on top of each other.
Create a constraints for vertical space for BlueView.bottom to
OrangeView.Top. Also create constraint for OrangeView to
superview.leading (or ledaing,margin) and BlueView to
superview.trailing.margin. If you select any one of these constraints and look at the Size Inspector on the right (the ruler) you should see an "installed" checkbox not selected, and below that a w-Compact h-Any and another installed box, this one selected.
Now, while keeping the constraint selected just to see what happens, change the sizeClass selector at the bottom to w-Regular h-Any. Notice that in the Document Outline to the left, it should get grayed out.
Now we are designing for regular, so side-to-side. Add constraints linking the views for horizontal space, BlueView.trailing to OrangeView.leading. Also link OrangeView.top to the superview.top or top aligned to BlueView.top, and same for bottoms. You can manually edit the frame first; if not, IB will automatically fill in the wrong values, so edit these after you create them, and verify they are w-Regular and h-Any. With the ViewController selected, select "update frames" and the views should snap to their expected shape for the size class.
Let us know if this works for you or if it was unclear. Good luck!

16 pixels margin left and right on UIView created with autolayout

I use autolayouts not for a long time and they often confuse me.
I have a wired constraints behaviour. I want to create a red view and make its margins 1/1/1/1. I tap a corresponded icon in the storyboard and set all fourth constraints to 1.
First of all values for left and right constraints are -16 and it's very strange. I have no idea where Xcode takes them.
I set all constraints to 1 and run my application. On the screen I see the view with top and bottom margins equal to 1 and right and left equal to 15.
And I have this behaviour always in every project. It very confuses me and don't give make UI correctly.
Does anybody know how can I avoid it? Everything I need it's say "Hey, Xcode, set margins for this view to 1/1/1/1" and the visible result will be the view with margins 1/1/1/1.
As usual, just after posing a question I've found a solution. When you set constrains turn the "Constrain to margins" checkbox off.

iOS7 auto layout, constraints, compression, content hugging

I’m having bit of a tough time getting my head around auto layout, constraints, priorities, compression and content hugging.
I think I understand it but getting it to play nicely seems to be impossible.
I have this simple view, with 3 buttons and a label.
I want them to be able to adjust to fit the screen when the user orients the screen.
All the buttons are pinned to the leading and trailing superview.
The top label’s top is pinned to the super view. The bottom button’s bottom is pinned to the superview.
There are vertical space constraints between the buttons.
I have tried varying priority levels for the compression of the buttons to get them to squeeze vertically for the horizontal view, but everything disappears off the screen.
Or one of the lower buttons appears above the upper buttons.
I am sure I am doing something quite simple to make these errors.
Would someone be able to offer me their 2 cents on how to fix this?
Many thanks for any help!
Merry xmas!
Adam
UPDATE - after "update all frames"
It seems button 01 disappears when the view is rotated
**UPDATE #02 - almost working but not sure how **
Guys I seem to have managed to get it to work, but i'm not really sure how.
And it's not reliable - if I adjust the constraints between the label and the first button then the oriented view ends up scaling off out of the bounds of the screen.
I would assume that I could adjust that vertical constraint to be able to change that space, but it just makes a mess.
Pin the bottom button to the view controller's bottomLayoutGuide, not the superview. Do this by control-dragging from the bottom button to the Bottom Layout Guide in the scene's Document Outline.
Also, your interface objects are misaligned; that is, the frames don't match the constraints (or the constraints don't match the frames). That's why you see the dashed rectangular outlines. Go to the floating tool bar in the lower-right corner of the storyboard canvas and click on the "Resolve Auto Layout Issues". From there, either select "Update All Frames…" or "Update All Constraints".
I think the default values for compression resistance and content hugging priorities are adequate.

Set fixed space between 2 views in Interface Builder

I am trying to learn to use layout constraints in interface builder but I am running across an issue...
I've got two views, one UILabel and one UIImageView, and I'd like a layout as follows:
the label should be centered in the superview
the imageview should be a set distance to the left of the label
Seems pretty simple, but I can't figure out how to impose a constraint for the second condition (the first is done automatically). Anyone know how to do this?
Thanks!
Click on both views while holding the command key so they're both highlighted. Then click on the pin button in the bottom right hand corner of IB (the middle button). Select horizontal spacing and the constraint should now be added. You should be able to adjust it as needed from there.

Resources