Align a middle object vertically in center between 2 other objects - ios

I need to put the red object align in center of middle between black and blue objects...
Which constraints should I add into these objects?

Ctrl + click on middle view and drag to top view , now leave click and Ctrl. select Horizontal center. Make sure you have given height and width constraints to both views as per your requirements.
Now Red and Black , Do same Ctrl + Click + drag from black to red or from red to black, add two constraints. Leading and Trailing like in second image
For adding multiple constraints to single object you may hold window key while clicking on attributes.
Hope this helps

If you only need to put red view aligned between blue and black view, you just need to set top and bottom space of red view with a same value.
If you provide enough constraint for them so xcode can calculate each of its frame, your red view would be automatically placed vertically centered between blue and black view.

You need to add a container view between blue and black views. Place the red view inside the container view and center this red view vertically inside the container view.
Steps (We'll only consider vertical constraints here):
1. Add a view (green view in the Screenshot 1).
2. Place the red view inside the green view (i.e, add the red view as a child view to the green view).
3. Add a vertical constraint between green view and blue view (Let the constant of this constraint be 0).
4. Add a vertical constraint between green view and black view (Let the constant of this constraint be 0).
5. Add leading and trailing constraints from red view to green view.
6. Add a "center vertically in Container" constraint from red view to green view.
Note:
1. I have set the background color of the container view to be green so that it is easier to explain. You need to change it to clear color so that container view is not visible.
2. You can see the Screenshot 2 for all the constraints between views.
Screenshot 1:
Screenshot 2:

Related

Autolayout with specific constraints

I have problem or maybe wrong understanding of constraints in general with view controller form photo below.
I already implemented scroll view and it is working properly. Photo below showing Content View with two subviews (with the same name: View). What I would like to achieve is to set first View (the blue one) to be exactly on edges of the screen and second one (green view) to be visible only when user scroll down on this page. Is it possible to achieve these just using autolayuots or I need to do some hard code work. What is the best approaches for solving this taking in account that I need this app to work with all king of iPhones.
At the bottom of the green view is located tab bar but it is not visible on this photo.
This is pretty straight-forward...
Pin your scroll view to Zero on all four sides.
set Blue Top, Leading and Trailing all to Zero to superview (the scroll view)
set Blue width equal to scroll view width
set Blue height equal to scroll view height
set Green width and height each equal to Blue width and height
set Green "center horizontally" to Blue
set Green Top to Zero from Blue bottom (vertical spacing)
set Green Bottom to Zero to superview (the scroll view)
That should do it :)
Edit: whoops, corrected step 4 (Blue height should be equal to scroll view height, not multiplied by 0.5)

How to set a view on top of two views using Autolayout iOS

I am trying to make a layout, where one view suppose to be on top of two views, in center that is half of first view and other half on second view's half.
I am not able to understand what constraint can i give for that. And is it possible to do it using attribute inspector or have to do it programatically.
My black view is equal width and equal height to white view with 0.75 multiplier. and x position is 0. Y position is what, i am not able to set.
constraint black view's centerY to top white view's bottom or bottom white view's top. If those two white views touch and will always meet at the center of the black view, this will work.
To do this in Interface Builder, ctrl+drag from black view to view one of the views and select center vertically. This will create constraint that ties black view's center to white view's center. This is not what we want yet, but we change that easily. Select this new constraint and under "Second item" select Bottom instead of Center Y, set constant to 0 and you are done

iOS - Top and bottom margin on control in horizontal UIStackView

I am designing a prototype cell in Xcode to display events in my iOS app. At the moment I have the following design:
The cell is constructed like this:
Stack View
Label (99:99)
View (Orange bar)
Label (Event title)
I would like to have a top and bottom margin on the orange bar of 2 pixels (basically not have the orange bar occupy the full height of the cell). I tried to add a top and bottom constraint on the View which looks like it is working, however Xcode is complaining about this solution (see screenshot).
Can anybody help me on how I could best accomplish this?
It is because of you have just set top and bottom constrain. you also need to add other constrain. Just click on red round. and set add missing constraint.
set constrain as shown in below.
Image of the constrain
Edit:-
View Hierarchy:-
Set View (Orange bar) height less than 4 pixels.After that set top space and bottom space of orange bar view to superview 2 pixels.

Align button with respect to background image in XCode

I have a background image and I need to put buttons in it. I have tried auto layout as well by fixing it's height and width (The button text was not part of the background, has been added manually), but it is not working for all screen sizes. I want two buttons equidistant from line that can be seen(like in iPhone 4 inch screenshot, line is part of the background). How can I go about doing that? Are there any other tools which might be helpful? I am using swift
Best way to do this is to add a hidden view which has a background color of clear. Add a constraint for this which centers it horizontally and a constraint which positions its top where you want it.
Make the width of this transparent view the space you wish between buttons or leave it as 1 wide if you wish and use offsets in the constraints described below instead. Make the height 1 so it takes up no space.
For the buttons align the top of the buttons with the bottom of the transparent view. This places the buttons vertically.
Finally pin the trailing edge of button 1 to the leading edge of the transparent view and pin the leading edge of button 2 to the trailing edge of the transparent view. Set the offset for the pins to be the distance you want the buttons from center if you gave your transparent view no width.
On all screen sizes, the buttons will now be relative to the center.
If you want to have them equally space out, you can add transparent views to the left and right of the buttons and pin them together so they act like spacers. So superview->spacer->button->spacer->button->spacer->superview.
Using the spacer approach, select the 3 transparent views and add a constraint for equal width. You buttons will now be spread apart equally.

Autolayout, retaining button spacing for larger screens

I have a setup like this in the interface builder:
(iphone5 size). I want the buttons to stay in that layout if the screen size increases i.e widens (but the buttons stay the same size). If I pin the top two buttons to their respective container edges when the screen is larger they will be too far apart from each other. I have the bottom button increasing its size fine. How can I do this? I tried putting a transparent UIView between each button and the container edge, but couldn't get it working this way. Could someone give me a pointer on how to do this please? thanks!
Assuming you already have the width, height and y-constraints in place, to create the correct x-constraints you can try something along the lines of this:
check the Horizontal Center in Container option
choose your button and double click the newly created constraint Align Center X to
change the Second Item from Center X to Trailing
enter a constant value to offset the position
Do this for both buttons. The constant value of the second button should be the negative of the first one AND its Second Item should be Leading.
Result
This method will result in equal spacing on all size classes.
The only drawback is that the spacing is no dynamic. for that purpose you would need some placeholder view in between.
I would say there are two approaches you could take:
Assumption is buttons have constraints for width and height.
1) Use a transparent view which you center horizontally within its containing view. Then pin the left buttons trailing edge to the transparent view and the right buttons leading edge to the center. You have a choice as to how to define their vertical position.
or
2) Add a transparent container view to your main view and then move your two buttons inside this view. Pin the two buttons apart the desired width and then pin the other button edges to the containing view at size 0pts. Finally center the new containing view horizontally in the view. You will need to define the height of this container view from the top or bottom.
The second is probably easier to execute within storyboard.

Resources