iOS Autolayout to Center My View Between Two Views

iOS autolayout to center my view between two views

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.

How to center two views in super view with greater than or equal to constraints

It's very difficult to center elements using VFL.

It's also difficult to center two elements unless they are embedded in a UIView or a UIStackView.

Here is one option by embedding the labels in a "container" UIView:

class MyVC: UIViewController {
lazy var titleLabel: UILabel = {
let l = UILabel(frame: .zero)
l.translatesAutoresizingMaskIntoConstraints = false
l.text = "Hello World"
l.font = .systemFont(ofSize: 50)
l.textColor = .black

// center the text in the label - change to .left if desired
l.textAlignment = .center

return l
}()

lazy var descLabel: UILabel = {
let l = UILabel(frame: .zero)
l.translatesAutoresizingMaskIntoConstraints = false
l.text = "description"
l.font = .systemFont(ofSize: 35)
l.textColor = .gray

// center the text in the label - change to .left if desired
l.textAlignment = .center

return l
}()

lazy var containerView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .yellow

// give the labels and containerView background colors to make it easy to see the layout
titleLabel.backgroundColor = .green
descLabel.backgroundColor = .cyan
containerView.backgroundColor = .blue

// add containerView to view
view.addSubview(containerView)

// add labels to containerView
containerView.addSubview(titleLabel)
containerView.addSubview(descLabel)

NSLayoutConstraint.activate([

// constrain titleLabel Top to containerView Top
titleLabel.topAnchor.constraint(equalTo: containerView.topAnchor),

// constrain titleLabel Leading and Trailing to containerView Leading and Trailing
titleLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
titleLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),

// constrain descLabel Leading and Trailing to containerView Leading and Trailing
descLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
descLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),

// constrain descLabel Bottom to containerView Bottom
descLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),

// constrain descLabel Top 10-pts from titleLabel Bottom
descLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10.0),

// constrain containerView centered horizontally and vertically
containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor),

])

}

}

Result:

Sample Image

Vertically center view between two others

Take a transparent view with top constraint 0 to topView(lable) and bottom constraint 0 to bottom view(Button). Now add the view in that view which you want vertically centre to both view. And add it constraints as horizontal and vertical centre to the parent view.

Center an item between two other items in autolayout (iOS)

The only way I have figured out how to do this is to create a container that holds the thing you want to center between the two items. That container should have the same trailing and leading amounts and then just center your "thing" in that container.

In the example below, the blue box would represent your orange "..." and the dark grey box would represent the container just used for centering. Once the layout works, just set the container to have a clear fill.

Sample Image

How to manage the gap between two views in auto layout

Take a UIView with alpha 0 and clear color and use it instead of the gap and use aspect ratio to the view used for gap and top and bottom or left and right whatever your situation to the views between whom you want to make the gap dynamic. And also you need to add some more alignment constraints to the gapView but it will work. I could not found any other method to do this easily so i have used this method everywhere in all projects and all working fine. I think UIView with clear color and alpha 0 will not effect the screen or performance in any way. We can use this.

Here is an example like if you have two text fields vertically and you want to increase the gap between them dynamically then take a view between then with the exact same width of text field and add those constraints.

1) Top of GapView to upper text field.

2) Bottom of GapView to below text field.

3) Equal width to any text field or leading and trailing to superview or main view.

4) Proportional Height to main view(you can add this constraint by adding equal height from subview to any of superview and then by changing the multiplier);

And your GapView will increase and decrease accordingly.

iOS: How to Align The Center of a View With The Bottom of Another View With AutoLayout

There are a lot of ways to do this. I'll just show one of the ways.

OK, Let's do this step by step.

Step 1

First, in order to align the center of the oval image with the bottom of the rectangular image, we need to embed the rectangular view in another view in which we could name as the Container View.

This container view will have the ff attributes:

  • transparent background color
  • width is equal to the screen width
  • height will be twice the height of the rectangular view(Later you

    will know why).

For the constraints of the Container View:

  • Leading is equal to super view's leading
  • top space to super view is 0
  • Centered horizontally in super view.
  • set aspect ratio with self.

Once you are done, the constraints will look like this.

Sample Image

Step 2

Now for the rectangular image view, the constraints would be:

  • Leading is equal to the container view's leading
  • Top space to container view is 0
  • Trailing is equal to container view's trailing.
  • Set aspect ratio with oval image view

    • By setting aspect ratio with the oval image view. The change in size of this rectangular view will be proportional with the change of size of the oval view.

It will look like this:

Sample Image

Step 3

This time, the contraints of the oval view:

  • centered horizontally in container view
  • centered vertically also in container view
  • set aspect ratio with self

    • Setting aspect ration with self ensures that when the rectangular view expands in size, the change in size of the oval view will not cause distortion.

This will be the result:

Sample Image

Step 4

There one very important view to add. This view will ensure that the rectangular view will always be half the size of the container view so that the center of the oval view will always be aligned with the bottom of the rectangular view. So, we call this view that we will be adding, the dummy view.

The dummy view will have the ff attributes:

  • transparent background color
  • width and height value should only be 1

The dummy view's contraints:

  • centered vertically in the container view
  • centered horizontally in the container view
  • width and height are fixed.
  • assign vertical spacing with rectangular view

This is how it should be done:

Sample Image

Alright, if you did the steps above you will achieve the effect.

This will be the overview of the constraints:

Sample Image

I hope this helps. :)

Proof that this works!

I ran it in the simulator... :D

iPhone 4:

Sample Image

iPhone 5:

Sample Image

iPhone 6:

Sample Image

iPhone 6 Plus:

Sample Image

Position an UIView between two views in the center?

I put two extra invisible views between x,o and o,y, and set equal width constraints for them.

I have now this layout: x-v1-o-v2-y

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

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

Sample Image



Related Topics



Leave a reply



Submit