I have two buttons contained within a view (to be precise, a UITableView footer):
I would like to keep the buttons centered (with the gap in-between) regardless of the width of the superview (which will increase, for example, if the device is rotated to portrait orientation).
Is there a way to define these autolayout constraints purely in the storyboard, or will I need to use NSLayoutConstraints in code?
You can achieve that by adding a constraint for each button to align the leading/trailing to the center of the container view, plus some separation between them.
How to do it:
For the left side button, add a constraint between the button and superview to center it horizontally.
Edit the constraint and change the "Second Item" attribute to be button1.trailing, and set a constant of 10 (or whatever distance you want between the right border of the button and the center of the screen).
For the right side button, add a constraint between the button and superview to center it horizontally.
Edit the constraint and change the "Second Item" attribute to be button2.leading and set a constant of -10 (or whatever distance you want between the left border of the button and the center of the screen).
Related
I am a beginner in objective c. I am trying to design a UIButton so that it's left side is always touches left side of screen and right side of UIButton should always be on centre of screen. I am trying it on storyboard with auto layout, but when I rotate the screen means UIButton doesn't meet the condition. What constraint should I have to use for this?
Setting up constraint is not so simple as it seems, you can achieve more with constraints too.
From your question i understood that you want UIButton from left side screen to cover half screen.
To do so:
1) Add left margin constraint with constant 0(zero), this will start your button from left side of the screen.
Now, how to make UIButton to fill half of the screen, You have to add little logic in you constraint.
button with to the centre of screen = screen.width/2
Here is how you can achieve with constraints.
1)Right Click on Button, drag to main view,
2)From list Select Equal width,
Usually this will makes your button same size of main view.
3)Select the width constraint of button (Must be in red Color)
4)Go to property inspector of constraint,
5) In multiplier filed change 1 to 0.5.
This is how you can make your button half size of screen.
In the diagram I need to center align the right view with the left view. If the right view is larger than the left view I need to top align it. Can I do this with constraints (storyboard) or do I need to do something in code to adjust the position (via a constraint) of the right view after the size has been determined? The Right view has multiple UILabels that can grow so I don't know the size until runtime. The container is actually a UITableViewCell.
Centering is easy. The right view resizes and the cell adjusts to that size. The issue is the requirement to top align if its too big.
You want this:
You can do it with two constraints.
You want the top of the green view to be greater than or equal to the top of the pink view, always. That is, you require that green.top ≥ pink.top.
In addition, you want the Y-center of the green view to be equal to the Y-center of the pink view, when possible. That is, you prefer that green.centerY == pink.centerY, if it doesn't violate the other constraint.
Here's the first constraint, in the storyboard editor:
And here's the second constraint:
Note that I have lowered the second constraint's priority to 800, which means it's not required but it's strongly preferred.
You don't need to write any code to make this work. I did write code in my demo to wire up the slider and the label, but I didn't have to modify those two constraints from code.
In the storyboard, align the right view to the left view top.
Give the value as
topAlignCons.value= +(leftView.height/2) //in storyboard
so that it gets align to the center of left view
Create an outlet for the top align constraint in your view controller.
When the right view is larger that left view, change the outlet constraint value to 0.
topAlignConstraint.constant=#0;
Now it will be aligned to the left view top with no negative value.
I was trying to put two buttons right next to each other, one taking up the left half of the screen, and the other taking up the right half of the screen. I found it perplexing that when I set Horizontal Center In Container for both of them that the left button had the property set correctly (center of button), but that the right button had the property set to the center of the View Controller!
I looked into this further and discovered that it takes this behavior whenever its on the right side:
In my pictures, I'm using a UIView with a black background, but it is applicable to anything. Is this the desired behavior? How else would I have two buttons laid out like I stated above?
Why do you use Horizontal Center In Container for your task? It should make both buttons appear in the centre of the container.
Do this instead:
Choose both buttons and set "Equal width" constraint.
For the left button, set leading constraint to the superview, and trailing to the right button.
For the right button, set leading constraint to your left button, and trailing to superview.
You can put your two buttons embebed in a UIView then put a Horizontal center Constraint in that UIView
So I have two UIButtons that I'd like to align, but not specifically the edges. I would like to align the (left) edge of one button to the (horizontal) center of another button. So I want it to look like this:
How would I do this AutoLayout in Interface Builder and/or programatically? I hope this isn't too much of a beginner question, but iOS AutoLayout can be so confusing at times...
In IB, create any horizontal constraint between the two buttons. For example, align their centers. Then, select the new constraint object and the Size inspector. There, you can change which attributes are aligned. Select the leading edge of the top button. Now the CenterX of the bottom button is aligned to the leading edge of the top button.
This is for XCode 5, which has sane autolayout generation.
If I drag, say, an UIImageView into a UIIView and want it to be centered in it's superview. I CTRL-drag the UIImageView onto it's superview and then shift-select Center Horizontally in Container as well as Center Vertically in Container.
This creates two constraints for where the constants are set to the current horizontal and vertical offset of the UIImageView. I've never had a case where this is what I wanted.
I then have to manually select each contraints, change the constant to 0, then trigger the view to update its frame.
Is there a way, with some keybind or a preference option, to make it so that I can create constraints that have a zero constant, instead of the view's current offset?
I haven't seen the case you're talking about where you get non-zero constraint values when you choose to add a centerX and centerY constraint -- that may be because we're adding the constraints in a different manner. The way I would add those constraints, would be to drag in the view, then click on the icon (second from left) at the bottom right of the canvas. Click on the check boxes for "horizontal center in container" and "vertical center in container", choose "items of new constraints" from the pull down control at the bottom of that window, and click "add constraints".