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
Related
I want to place an UIImageView at the half of an another UIView. I have set the constraints like this:
I need the square image view to be at the half of the orange UIView . The above position of UIImageView is correct but it slightly moves up in 6 and SE screens.
Please look at the screenshots:
Why it is happening and how to prevent the UIImageView from moving up ? Do we need some other constraints?
What should be the constraints for the Green UIButton Placed to the right side of the UIImageView?
First you need to remove your top space constraint, then you need add a constraint between your UIImageView and your redView, right click and drag to your red view and select align to to Top, later you need to change the first item from .top to .centerY, and add a multiplier value of 1 to be center in your red bottom edge
One Picture worth more than 1000 words
Second requirement
In your second requirement, you need add an helper view I named it guideView this view background will be .clear and userInteraction will be disabled, then guideView will have .top and .bottom constraints to your UIImageView .top and .bottom also will have trailing space to superView with constant 0 and leading space to your UIImageView with constant 0, as shows this image, then your desired green view go inside of guideView and will be aligned center in Vertical and Horizontal to superview and height and width with fixed values and that is all
Hope this helps
It can actually be done with a single constraint. Just align the center of both your views like this:
If you also look at the remaining constraints, the original view must be already properly constrained, the second view that have to be aligned to the original requires width and height provided and also position of the other axis. In this case where Vertical centers are align you will need to provide a X position.
Therefore if you want the views instead be aligned one above the other, align the Horizontal Centers instead and provide the Y position of the views.
You could also do it programmatically. This will always center the image view on the top edge of the view, with half above and half below.
First, make outlets for the UIImageView and for the UIView.
Then:
[imageViewOutlet setCenter:CGPointMake(viewOutlet.frame.size.width/2, viewOutlet.frame.origin.y)]
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.
How to Align Two UI Button Horizontally Aligned in A Xib Using Size Class and Auto Layout. Two Button should keep its original height and width
i would like to achieve this without code
Thanks in advance
First add gallery button:
Height constaint
Width constraint
Leading space with superview
Top space with superview
Then add camera button:
Horizontal space with Gallery button
Equal width with Gallery button
Equal height with Gallery button
CenterY with Gallery button
And it should be done.
Check this video, to set the constraints
You can check in the preview,
Select both Buttons.
Add the following constraints show in image
This will add default height depend on Text Font.
So if you want to increase height then Select any one button and add extra height constraint. Update the frame.
Just simple select Two Button and Give horizontal and vertical spacing constraints and give Equal Width and Equal Height to Button.
Connect your Gallery button to your main view's corner and set it using
Next, connect your Camera button to Gallery button and set Horizontal Spacing.
Inside your Storyboard's Leading Alignment Contstraint, set Gallery.Trailing as First Item, and Camera.Leading as Second Item. Set Relation to Equal.
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.
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