Horizontal Center in Container bug in Storyboard of Xcode 6? - ios

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

Related

How to place UiButton's right side is always on centre of screen?

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.

iOS layout: How to keep a button in the middle an image?

I have an image that needs a UITextField in the middle of it at all times. However, I can't get it do what I want.
I pinned the image with top, left and right constraint and gave it a height constraint. I put the UITextField in the middle of the image and tried various methods to keep it in the middle, but they all keep failing. Any tips are appreciated. Here is what the image currently looks like. First one iPhone 4 and second one is iPhone 6
You can just select the image view and the button and create two constraints: a “Horizontal Centers” constraint and a “Vertical Centers” constraint. Neither view has to be a subview of the other, and they don't have to be in the same superview either.
Here's a demo. I've already constrained the image view to fill the top left quadrant of the root view, and I've dragged in a button. In the demo I'll select the image view and the button (hold shift to select multiple views) and create the two centering constraints. Then I'll use the Preview assistant to show that the button stays centered when the image view's frame changes.
What you need to do:
Embed the UIImageView and UITextField inside a UIView.
Set constraints of UIImageView leading, trailing, top, and bottom to UIView with constant of 0.
Set constraints of UITextField center horizontally with container view, and center vertically with container view.
Then it's up to you on where you want to position that UIView. This will definitely always position the UITextField to the center of UIImageView.

Align 3 UIButtons to the bottom centre of UIViewController

I have a UIViewController that consists of a UIImageView then I want to provide three options to user namely, rotate, crop, draw.
I want these three buttons to remain always centred at the bottom and maintain an exact distance with the UIImageView.
How can achieve this using auto layout I have done a lot of search as well as work but to no good use.
Set constrain like this ,
First set Constrain to centre button(Crop Button). like,
Crop button :- Bottom spacing from bottomlayout , height and width (According to your requirement), and Centre Horizontally in container (now your crop button is fix in centre)
Rotatebutton:- set Horizontal spacing from crop button , rotate buttons top=cropbuttons top , rotate button's bottom=crop buttons bottom , and Rotate button's height and width equal to Crop button's Height and Width.
(set constrain to draw button same as rotate button)
So, all button's look same .
check in image ,
1) Set constrain to Buttons
2) Now set constrain to imageview
3) Output
Edit :- Here i have attached demo ,
Demo for Centre button
(1) First add ImageView, constraints like this
(2) now add an UIButton in the bottom center named CROP. Give Constraints like this
(3) Now add 2 UIButtons named ROTATE and DRAW on either side of CROP
for ROTATE:
for DRAW:
(4) Finally select 3 buttons and give equalWith Constraint
No need to add Constraint to with ImageView. Add these buttons on a UIView and add the constraint on that view as Horizontally Center and also add ImageView on a UIView. Now add the constraintHorizontally Center on the parent view of Button to the parent of ImageView.
Your scenario should be like below,
left most button should have two constraints(cancel) : leading,bottton
right most button should have two constrains (yellow button) : trailing,bottom
Take one UIVIew and put thhree buttons in that view with equal distance from every side of that uiview.
now your constraints to all three buttons should like : leading,trailing,top and fixed height
And constraints of that uiview should be : horizontally center in container, bottom and fixed height
I am attaching screenshot aswell
You can check constraints from view hierarchy on left side.
I have set gray background color to just demonstrate this is view you not need to set color of that view.
By this way your three buttons will always display to bottom center of your VC
hope this will help :)
Hope the below steps might fix the buttons the way you wanted.Have not tried myself but you can try out if you have not tested these steps.
1.Pin those buttons to the bottom space container
2.Add a constraint from button to top space to image view
3.Play around horizontal spaces between buttons by pinning leading edge to the view for the left most button and trailing space to the view from right most button
4.Check center horizontally/vertically to the container

Positioning two UITextFields side by side using Autolayout

I am new to Autolayout, so I am struggling to understand how to position two UITextFields side by side. E.g.:
I have tried a number of constraints, but I cannot figure out how to correctly position them. When running the app.
Any insight on how to do this?
Try pinning the top, left and bottom of the check in textfield to the superview. Pin the top, right and bottom of the check out textfield to superview. And the left constraint of check out to check in using the 'pin' button (bottom left of the storyboard), a box between two lines icon.
Other option is to create a Stack View with these TextFields
You have the views like this now:
Now, you have to select the Constraints to the Stack View:
And then you only have to select Fill Proportionally in the Distribution:
Final result:

Keep UIButtons centered using autolayout in storyboard

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).

Resources