How to edit/modify NSLayoutConstraint in Xcode? - objective-c

I drag a tab view into my custom view, and set its frame. Xcode automatically generate several NSLayoutConstraint objects those will decide its frame during auto-layout.
As shown above, a constraint which indicates the distance between right edge of the tab view and the RIGHT edge of its super view.
However, I do not want a constraint like this. What I want is a constraint which indecates the distance between the right edge of the tab view and the LEFT edge of its suber view. OR, the width of the tab view itself.
How should I do?
BTW, I tried delete the constraint, but Xcode generated this constraint back immediately. Therefore I could not add one (and actually I do not know how to add one, either).

There is a minimum number of constraints required on each axis of the view, hence you should add a new constraint first before deleting existing one to make sure Xcode understands how to layout the views properly.
Try to add a new constraint by clicking a view and then click on the middle button at the bottom right options in your screenshot:

go through following link, which explain auto layout..
http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2

Related

UISearchbar scope is hidden behind the search text zone after adding constraint

I've come up against a problem I cannot sort out. I have a nib file where I added a table view and a UISearchbar with a scope bar. Everything was working perfectly until I added constraints to all the views (tableview, searchbar, a few labels and buttons). Since that moment, I cannot see the scope bar anymore. It seems to be hidden behind the searchbar because I can see a kind of blue surrounding the search area where text is entered (see below):
Does anyone have already been facing this problem?
EDIT:
This is the image with the constraints mentioned as a possible issue:
There might be either of below two constraint causing the issue.
Height constraint to the searchBar - you can resolve this by deleting the height constraint
OR
If your tableview has top constraint to the superview you will need to either delete it or you will not increase it till you see the scope bar.

How to implement scroll view in xib cocoa objective-c

In order to implement scroll view I do:
Create Cocoa Application
Go to XIB
Drag scroll view to the view window and set its constraints to 0
Everything seems fine until now
Under Bordered Scroll View (in the Document Outline) I press on Clip View and then View and resize that to any large number (under size inspector)
I add a button (for the sake of it) to the view (under clip view) (in the Document Outline) and sets its constraints
After this I immediately get the "Ambigious Layout. Position is ambigious for "View".
What am I doing wrong? Is this the proper way to add scrollview? It also seems rather difficult to add items to the scrollable area as I dont see the entire scrollable area in the xib.
Please help a noob.
In general this is a correct way to add a scroll view. (You can also create a view or set of sibling views, select them, and choose Editor > Embed In > Scroll View.)
If a view has no constraints, then Xcode will add sufficient constraints at build time. These constraints are not necessarily the ones that cause the view to behave like you want as things change size, but they're good enough to maintain the current layout of the canvas when things have their current sizes.
However, once you add constraints, Xcode will start insisting that the constraints are mutually-compatible (no conflicts) and sufficient to be unambiguous.
So, that explains why you get that warning. You have added some constraints, but not enough to make the layout unambiguous. You need to add enough. Xcode should explain in more detail what's needed, although there will necessarily be multiple possibilities for how to resolve the ambiguity.
In your case, I'm guessing that the size of the view in the scroll view is ambiguous. For example, you may have added constraints to position the button relative to the top and leading edge, and the button likely has intrinsic size, but you haven't constrained the view's bottom or trailing edges to the button. So, the size of the view could be anything.
Of course, rather than constraining the view's bottom and trailing edge to the button, you could just add explicit height and width constraints to it. Or whatever.
You may also need to constrain the view to the clip view.
You can also use Editor > Resolve Auto Layout Issues > Add Missing Constraints and see what Xcode adds. You can then change things from there if what Xcode added is not what you want.
For my Mac OS X app, I selected the controls on xib to embed in scroll view ( Editor > Embed In > Scroll View ) and applying the following constraints to Custom View (inside Scroll View -> Clip View) did work.
Where hight is to accommodate controls.

Xcode, why some constraints are automatically added?

Let's say i want to align a button (Delete) on the right border of every different device (3.5, 4, 4.5 inch etc.)
Should be very simple but, when i add a constraint to the right border:
New constraints are automatically added and i can see a dashed border rectangle on the top right of the view (which means that my button has been moved to the top.):
Why?? I don't need to add tons of contraints, i just want to align the button to the right and stay in the same position for every device.
You're seeing the red outline in the upper right corner because now that you've added one constraint Auto Layout needs additional constraints in order to know that you also want the button further down on the screen. You can accomplish this by also setting a constraint for position from the top, for instance.
Xcode is not "adding tons of constraints". It is doing what you said to do - and showing you that the results of doing that would be a disaster. It is telling you that you need to add more constraints or you'll have a mess on your hands.
Basically, as soon as you add one constraint to position/size an object, you must add sufficient constraints to position/size it unambiguously.
(Notice the warning/error messages in the document structure pane at the left, not shown in your screen shots. They are telling you something!)
You added constraints for the x position of the button. Now you have to add constraints for the y position of the button. An easy way that will most of the time work is to go into the auto layout bar at the bottom and clicking the third icon from the left. Then you click either add missing constraints or reset to suggested constraints.

Enforcing Auto Layout Constraints Across The View Hierarchy

I have two buttons that I want to be kept the same size, but the problem is that they have two different parent views. Autolayout seems to be ignoring the "equal size" constraint in this scenario. Constraining buttons with the same parent view works just fine.
I've created a very simple example that depicts what I am seeing:
As you can see from the above, buttons Two and Three are both set to have the same size constraint as button One. The only difference is that button Three is contained within another NSView. There are no width constraints that are linking button Three and its containing view.
However, when I run and resize the window, it looks like:
It doesn't matter whether I use the Interface Builder layout, or do it in code using the -[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:] method.
What am I missing? Is this not supported by Autolayout? Thanks a lot.
Edited: Added screenshot to indicate button Three's constraints.
This should work OK. I set up an example project to do the same thing. I have three buttons with equal width constraints between button one and button two. Then the same between button two and button three. Button one has a trailing edge constraint relative to it's parent view.
Note that between the second and third button, Interface Builder didn't let me do this in the main window. Instead, I had to do it using the document list on the left.
The result can be seen below:
Here's the link to the project:
https://github.com/MaxMacleod/ThreeButtonConstraintTest1
Couple of caveats. First, this is an iOS rather than an OS X project (I'm an iOS guy!). However the principles should be the same. Secondly, this doesn't pinpoint the exact reason why your project isn't working. However, if you can compare what this sample project does against yours, we can figure it out. I'll then update this answer. Better still if you could make your project available, I'd be happy to take a look.

Is there a way to shrink a constraint with autolayout without code?

Without using code, I'm basically trying to achieve the "Desired outcome" in this picture:
I want the constraint on the "Hello!" label to shrink its length when the screen height is shorter.
As you can see in "Actual result", the Button in the bottom is off the screen. I want the image view to have fixed width and height.
I know I can create IBOutlets for constraints and doing it by code like this kind of posts suggest (autolayout - make height of view relative to half superview height), but I'm trying to avoid using code as much as possible.
Side question: If there's no way to do this in IB, what are the best ways to do this in code?
Thanks for the help!
Yes. The easiest way to have this kind of "split height" constraint is to put a "header" view between your image view and the top of the screen, and embed your "Hello" label inside this new header view. Then add a constraint to keep the "Hello" label vertically centered inside the header view.
To keep the header view the right size, add constraints to keep the top of the header view pinned to the top of the screen, and the bottom of the header view to the top of the image view.
Then you'll just add constraints to keep the button and image view pinned to the bottom of the screen. (Or, see comment from Sulthan, below.)
In Xcode 5.1 (in beta), there is UI to make more general constraints, including the ones you want, but you have to do it like this or in code in earlier versions.