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

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.

Related

Auto resizing constraints

I try to create UIView with auto resizing constraints. The goal is so simple. I need UIView that will transform its size according to device screen. I created three constraints that align to safeArea and one with offset. But when I change Device in a storyboard view, it always appears some gap in the bottom constrain.
In this view, everything is ok. This where I created all constraints
But in all others it's terrible gap.
Does anybody have an adea how to fix that? Unfortunately I don't understand where I did mistake.

Handling autolayout special case

I have established constraints to like buttons, timestamp button, etc. below bottom of textfield a certain distance. You can see the issue in the image; if the textfield doesn't have enough letters, they overlap, how do I fix this? Here's a link to image:
https://docs.google.com/document/d/1_btZBSsL3SQA1IulFTasMFBghF_2hJYzrFlbuQPkKrM/edit?usp=sharing
The problem is setting the constraints with respect to the bottom of textfield. That bottom moves depending on the amount of text the field has and if that bottom position is above the bottom position of the icon to the left of the text field, you'll get the overlap you see. The solution, you might think, is to set the constraints with respect to the bottom of the icon but that won't work when the text field has a lot of text. You should try that just to understand the problem more clearly.
The actual solution is to set this particular constraint programmatically, as text is entered into the text field. You want to set that constraint with respect to the bottom of the view (icon or text field) that has the lowest bottom. That way, with little text, the constraint is set relative to the bottom of the icon but when there's enough text for the text field to vertically extend below the icon, the constraint is set relative to the text field instead.
Where would you set this constraint programmatically? Probably in the textField:shouldChangeCharactersInRange:replacementString: delegate method of UITextField.
Alternatively - and perhaps a better solution - you could have a view containing the icon and the text field and set the constraint in question with respect to the bottom of the container view. That way you can set the constraint in IB rather than have to do it programmatically.
To clarify my answer, after the discussion in the comments below, here's a picture of what I mean by having container views and setting constraints between them to avoid the observed overlap.
Set appropriate constraints within each of the container views then set constraints between the container views and between them and the cell content view (I'm assuming, from your picture, that you have all of this in a table view so you have some template table view cell).

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.

How to edit/modify NSLayoutConstraint in Xcode?

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