Constraints for Centering One Button When the Other Is Hidden

Constraints for Centering One Button When the Other Is Hidden

Embed both buttons within a UIStackView:

Sample Image

and adjust its settings to your needs:

Sample Image


Result with button1 hidden == false (un-ticked):

Button1 hidden == false (un-ticked)

Result with button1 hidden == true (ticked):

Button1 hidden == true (ticked

Swift auto layouts change button position when some buttons are hidden

UIStackView will do what you want.

Give it Top, Bottom and Trailing constraints, but NOT a Width constraint.

As you add / remove arranged subviews, it will remain "right-aligned" in the cell.

Here, I have 4 UIImageViews "a" "b" "c" and "d". The image views have width constraints of 30, and aspect ratio constraints of 1:1.

First, all 4 image views are in the stack view:

Sample Image

Next, I've removed "c":

Sample Image

And now, I've removed "a" and "c":

Sample Image

As you can see, the stack view handles the placement of the image views for you.

How to constraint two buttons to be equidistant from the vertical center

@Gereon's answer is better than @conarch's, IMHO, because spacer views are almost never the right approach. I have a third approach, though, that I find to be more robust than @Gereon's because it doesn't rely on hard-coded constants.

  1. Enclose both buttons in a UIView (Editor -> Embed In -> View).
  2. Constrain the left button's left edge to the left edge of the parent view.
  3. Constrain the right button's right edge to the right edge of the parent view.
  4. Constrain the left button's right edge to the right button's left edge and give it the desired spacing constant.
  5. Constrain the tops and bottoms and/or heights as you see fit.
  6. Align the horizontal center of the parent view to its parent view's horizontal center.

How dynamically align button in xCode

Use StackView. You don't have to think about any layout constraint.That is center aligned. When you hide something from stackView it will automatically get centered according to your need.

Swift constraint doesnot change in Autolayout while hiding views

You need to take the outlet connection of the vertical spacing constraint between either First-Second or Second-Third views. Also if you want to hide/show only Second view, you don't need to do any changes to Third View height constraint.

Say For example, we take the outlet of vertical spacing between First and Second views, then:

@IBOutlet weak var UIVIewFirst: UIView!
@IBOutlet weak var UIViewSecond: UIView!

@IBOutlet weak var middleViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var verticalSpacingConstraint: NSLayoutConstraint!

@IBAction func infoClicked(sender: UIButton)
{
if UIViewSecond.hidden
{
sender.selected = false
UIViewSecond.hidden = false
self.middleViewHeightConstraint.constant = 134
self.verticalSpacingConstraint.constant = 10
}
else
{
sender.selected = true
UIViewSecond.hidden = true
self.middleViewHeightConstraint.constant = 0
self.verticalSpacingConstraint.constant = 0
}
}

Below are the screenshots of output:

1. When button is not selected

Sample Image

2. When button is selected

Sample Image

Centering a group of objects in between other objects in Xcode

Use stackViews.

1, Create 4 horizontal stack view for you "lines". First select top 2 buttons, and add it to (horizontal) stackView. (Bottom-right corner on the storyboard editor). Do it with other lines as well.

stackView

2, Set the stackView's distribution settings to Fill equally and add custom spacing.

stackViewSettings

3, Add all the stackViews to one stackView (this will be vertical), and set it fill equally as well.

4, Set up constraints for all the stackViews (left: 0, right: 0)

cons

5, Select Main stack view and setup for horizontally and vertically centered.

center

Result

result

How to use Auto Layout to move other views when a view is hidden?

It is possible, but you'll have to do a little extra work. There are a couple conceptual things to get out of the way first:

  • Hidden views, even though they don't draw, still participate in Auto Layout and usually retain their frames, leaving other related views in their places.
  • When removing a view from its superview, all related constraints are also removed from that view hierarchy.

In your case, this likely means:

  • If you set your left view to be hidden, the labels stay in place, since that left view is still taking up space (even though it's not visible).
  • If you remove your left view, your labels will probably be left ambiguously constrained, since you no longer have constraints for your labels' left edges.

What you need to do is judiciously over-constrain your labels. Leave your existing constraints (10pts space to the other view) alone, but add another constraint: make your labels' left edges 10pts away from their superview's left edge with a non-required priority (the default high priority will probably work well).

Then, when you want them to move left, remove the left view altogether. The mandatory 10pt constraint to the left view will disappear along with the view it relates to, and you'll be left with just a high-priority constraint that the labels be 10pts away from their superview. On the next layout pass, this should cause them to expand left until they fill the width of the superview but for your spacing around the edges.

One important caveat: if you ever want your left view back in the picture, not only do you have to add it back into the view hierarchy, but you also have to reestablish all its constraints at the same time. This means you need a way to put your 10pt spacing constraint between the view and its labels back whenever that view is shown again.

keep 3 UIbuttons center aligned using autolayout

Just Check out accepted answer of @Usama : Align Three UIButton using Auto Layout

Here is the Sample Code

Sample code output:

Sample code output

Thanks to : Usama

Multiple buttons at center of screen vertically with Autolayout in iOS

Sample Image

Sample Image

1 select the Button2 give hive constraints like first image.see the constraints window.

2 give the constraints to butto1.
Sample Image

3 give the constraints to butto3.

you can see all there button constraints in window.



Related Topics



Leave a reply



Submit