iOS autolayout to center my view between two views - ios

How to set up autolayout in Interface Builder to arrange views vertically as shown on image:
Top view pinned to top screen edge, Bottom view pinned to bottom screen edge. My view should be centered between top and bottom views (so distance to Top view equals to distance to Bottom view)

The way to do this is to have 2 invisible "spacer" views between you visible views.
You can't make spaces have equal height. But you use the "spacer" views and place them between your views (with 0 gap to the surrounding views).
Then you give these views equal heights and they will push your views around to centre the My View with equal gap to the Bottom View and Top View.
i.e. like this...
V:|[Top View][spacer1][My View][spacer2(==spacer1)][Bottom View]|
EDIT - Another way
I just thought of another way of doing this. You could have an invisible container UIView that is between Top View and Bottom View with no gap.
Then you place My View inside this invisible view and centre it vertically.

One more solution is to add an invisible view between top view and botom view and place my view in center of this view:

(5 years later) I've found the most canonical solution:
UILayoutGuide is specially introduced to solve this kind of tasks (since iOS 9).
Use layout guides to replace the dummy views you may have created to represent inter-view spaces or encapsulation in your user interface - Ref docs
It works pretty fine in code, but unfortunately Interface Builder doesn't support custom layout guides.

Related

Vertically centering text with an image in a horiztonal stack view (AutoLayout) - iOS

I've been having some problems centering text vertically beside an icon in a horizontal stack view. Here are some facts:
1) I create a view to hold stack view - add constraints
2) I place a horizontal stack view inside of that view - add width and height constraint, add vertically center constraint, add height constraint
3) Add an image and a label to the horizontal stack.
4) I make the image a certain width and match the height
5) I leave the label alone
6) I select the stack view and specify to set the Alignment to Center
In a new view controller this works great! Everything is perfectly aligned vertically. BUT in any existing controller I've created, it doesn't work! The text is rendered a few points above the center of the image, AND not at the top.
The only difference I can see is new view controller vs existing view controller, and simple layout compared to my more complex layout.
I have no auto layout errors, and don't really consider recreating all my view controllers to be an option. Anyone have any ideas?
Wow, so I have this partially figured out. Its the custom font we are using for our app!
This was the primary difference. All my normal app views inherit from a base view where I set the font for the entire view. That is why when I tested in new view controllers (not inheriting from base) it would work.
If you are having trouble getting text perfectly vertically centered and you think you have done everything right, check your font.
Happy coding...

AutoLayout : Relative scaling of views

I want to achieve consistent views across different screens. My layout constraints works on small screens perfectly but it scrambles on large screens.
I have made a view(Red Border indicating that view)and pin it in to the main super view. Then I have made sub views and put it inside that view. I have pinned the the top bottom trailing and leading edge of the layout which contains button of the it's parent view (Red Border indicating that view). The view containing button is looking like this in small screen (iPhone 5)
while it's looking in big screens like this (iPhone 6 and above)
I want this layout to have the same relative height in all the screens like the other views. How can I achieve this?
I guess you can use UIView that contains a button inside and set bottom,lead,trailing,top constraints for the UIView. Then you can set height constraint of that UIView and set multiplier values to have relative height to its superView(=Red box). Finally, you can set constraints for your button inside the UIView whatever you want. If you know how to use StackView, I recommend you to use it because it is easy and simple solution. Here is very good tutorial about StackView.
If height is constant for subview except that button view the layout will be like what you mentioned in the first image .If you kept height as constant for that button view the problem will be solved

Auto Layout centering view in remaining space (programmatically)

How can add Auto Layout constraints programmatically to center a view in the remaining space (see example picture below)?
For the moment, I add a container view at the bottom and I then center the view in the container view, but I was wondering if there was any other solution without needing to use a container view.
You need to add a spacer view to do this.
Let's start with some views:
I'll set up the pink view to take up the top 70% of the root view. First I pin it to all four edges of the root view:
Then I'll edit the bottom constraint in two ways. First, I make sure the first item is the pink view, and second I set the multiplier of 0.7. Then I update the pink view's frame:
Next I'll add the spacer view. I don't want the spacer view to be visible at runtime, so I'll make it hidden. Hidden views still participate in layout. Before setting up constraints, I just put the spacer to the left of the blue view:
Now I'll create constraints to make the spacer stretch from the bottom of the pink view to the bottom of the root view. The width doesn't matter so I'll just pin it to the left edge of the superview and make it thin:
Now I'm ready to set up the blue view. First I'll give it a fixed size:
Second I'll center it horizontally in the root view:
Third I'll pin its vertical center to the spacer's vertical center:
That's all the constraints I need. I'll update all frames to check:
I can test it out using Preview in the assistant editor:
Notice that the spacer view isn't visible in the preview, but still participates in layout.
The accepted answer doesn't address the programmatic alternative (which you emphasized in your question).
There is a programmatic way to do this without having to add additional dummy views to the view hierarchy using UILayoutGuide which was introduced in iOS 9.
The documentation for UILayoutGuide is detailed enough.
This article also add more explanation (Here is the updated code from the article).

iOS - Using storyboard and autolayout to center the UIScrollView

I'm creating iOS app using story board and auto layout so that it will work good on both iPhone4 and iPhone5. Below is the screen shot of the view that I'm creating using story board.
In the above image, I want to keep the scroll view in the middle from leading edge of superview and the right table view. I dont want the scroll view to increase its width in iPhone5. I tried different combinations of constraints, but I couldn't achieve it.
Can some suggest me what are all constraints that I've to set for scroll view so that it will be in center.
How to Center a View on the Storyboard
This answer has been updated for Xcode 8.
This answer is for people who are just doing a general search for how to center things using a storyboard and don't understand constraints.
I assume you are using a Storyboard (not XIB files) and Auto Layout. This is the default nowadays so if you don't know what that means then don't worry about it. If you want to check, though, you can click Main.storyboard in the Project Navigator and then the File inspector. You can make sure that Use Auto Layout is checked:
Select your button (or whatever view you want to center) on the storyboard. Then click the align button on the bottom right. Select "Horizontally in Container" and "Vertically in Container". Click "Add 2 Constraints".
If it wasn't perfectly centered already you may need to do one more thing. Click the "Update Frames" button that is two to the left of the align button. Your view should now be centered on the storyboard.
And more importantly, now when you run your app it should be centered no matter what device size you are using.
I originally learned this from Creating the user interface – Auto layout. The basic idea is still the same, but the UI arrangement has changed a little in Xcode now.
See also
How to set constraints programmatically
You will need to do this by adding an additional view to the screen.
At the moment you have...
- UIView (main view)
|
| - scrollView
| - tableView
You should put the scroll view inside another view like this...
- UIView (main view)
|
| - UIView (spacer View)
| | - scrollView
|
| - tableView
Now what you can do is have these constraints...
spacer view leading edge constraint to super view = 0
spacer view trailing edge to table view leading edge = 0
table view width = (whatever the width is)
table view trailing edge to super view = 0
This will lay out the spacer view and the table view so that the spacer view will grow.
Now you need to add...
scroll view width = x
scroll view height = y
scroll view centered vertically in super view
scroll view centered horizontally in super view.
Now, because the scroll view's super view is the spacer view then it will always be centered in between the table view and the rest of the space.

Centering View between neighbors using autolayout in IOS

I am attempting to layout a screen design using constraints (auto-layout) from interface builder and hope to avoid coding constraints, but I would accept an answer for either case I guess.
I have two subviews in a contained in a top level view. I want to fix vertical distance of the first subview to the top of the screen (I was able to accomplish that - common use case for a vertical space constraint). I want the second subview to float (vertically) in the center of the distance between the bottom of first subview and the bottom of the screen. The idea is that the design responds somewhat to iPhone 3.5" vs 4" dimensions.
I am having trouble defining a constraint or set of constraints that would accomplish this.
I have tried setting inequalities on the vertical spacing constraints between the second subview and the top of it's neighbor (the first subview) and the bottom of the superview, and playing with the priorities of those constraints.
An idea that was suggested in a few other related posts on SO is to use a container that is pinned to the bottom of the screen, as well as the pinned to the bottom edge of the first subview, and then center my second subview in the container. I was hoping to avoid complicating the view hierarchy if possible, but maybe that isn't easily avoided.
Any thoughts or suggestions?
UPDATE
This answer describes how to do this in Xcode 6.0 (and probably works in Xcode 5.0 and 5.1).
ORIGINAL
If you want to do this with auto layout, you have to add at least one spacer view. It doesn't have to be the superview of the vertically-centered view (or any other view), so it has a pretty minor effect on your view hierarchy.
I recommend not trying to set up constraints in Interface Builder in Xcode 4.6.3. It's just too painful. But if you really must, this is doable.
Create a spacer (a plain old UIView) from the bottom edge of the top-hugging view to the bottom edge of superview. Set it to hidden. Give it constraints to the top-hugging view and the left and bottom edges of the root view, and pin its width. My spacer width is 10:
Add your middle view (here, a button). Give it a “Horizontal Center in Container” constraint. Then select both the middle view and the spacer view and give them a “Vertical Centers” constraint:
Note that if you drop the middle view when IB is showing the correct guidelines, it will set these constraints for you.
You can test by enabling resizing for descendents (but not siblings & ancestors) and resizing the root view in IB. The middle view will remain centered between the top-hugging view and the bottom of the superview:

Resources