I have a simple UIImageView with 50x50 size.
If I had a UIView of size 100x100 and I wanted to center the image inside of it, all I have to do is set these
But how do I do the same with a UIStackView of size 100x100?
EDIT i think i should have been more clear. The questions is, how do i center the image both vertically & horizontally inside of the UIStackview ?
Use two stack views.
Vertical stack view with centre alignment.
Inside that is a horizontal stack view with centre alignment t.
Inside that is your image.
You need to set the alignment to center inside attribute inspector of UIStackView
Check attribute inspector for stackview settings:
follow these steps.
1) drag Uiimageview.
2) click on UIimageview. Press Ctrl and drag downward or sidewise to parent view.
3) click on "centre vertically in view" and centre "horizontally in view"
4) then set height and width like below.
Thats it. your imageview will be at centre regardless of the device type.
good luck.
leave a comment if you any question. Accept the answer if it helps you.
Related
I have to show the Name and EmailID aligned horizontally properly with the Profile Image . So that the Name and email ID appears exactlybat the center of the UIImageView.
But you can see the name and email id are not in centre with the profile image. Why is this happening? I have given the following constraints:
Remove leading and trailing Constrain and set align center x to imageView.
You can also set leading and trailing Constrain. but both are equal as follow.
Just control+drag from label to imageview and select Center Horizontally.
Ctrl drag from the labels to the imageview and choose Center horizontally.
I know the question has been answered but this is best solution horizontal stack view
Ctrl+drag from image and labels to their superview and select Center Horizontally in Container.
Using AutoResizing Tool:
Step1: Place your image view and labels exactly how do you needed.
Step2: Select Image view and click on Size Inspector.
Step3: Simply click on red bars of autoresizing subview to de-select it which is shown in below image.
Step4: Repeat same steps for labels. I made labels text alignment as centre.
I have a Horizontal StackView inside vertical StackView.
Horizontal StackView contains a image (w=30 , h= 30) and a label.
Need
I need to make image & label to center horizontally as per label intrinsic content size.
So, it looks like ----Space----- image & label -----Space---
How can I achieve that?
Thank you,
i am using only horizontal stackView to make it center.
What i did.
Step 1:
make my image with specific width and height.
Step 2:
Both are inserted in Horizontal stackView and make that stackView center aligned.
Edited:
If i use vertical stack View On top, i can't find any problem for centering label and image
You can use UIView inside Horizontal StackView then add image and label on UIView
Please take a look on attached image
Because the VerticalStackView forces the HorizontalStackView to have same width, So you have to wrap imageView and label inside a UIView. Like this
Add two extra views, I named it View 1 and View 2(green color) on left and right of your image and label, set two views width equal so your imageView and label will automatically center, don't forget set width for imageView and label. Set view 1 and view 2 clear color or under other view view if needed(to see background, other views, interaction with other views...).
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.
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
I would like to centre a UIImageView inside a rectangular region on an iOS screen. The location and dimensions of the rectangle are determined at runtime and are represented by a CGRect. The image has already been drawn but has to move to the centre of the rectangular region at runtime. How can I achieve this?
It depends. If you are using auto-layout (the default) then you would use layout constraints. You should be able to set up layout constraints in IB that would keep the image view centered even if it changes size, without requiring any custom code.
Simply Select the view in IB, choose the button at the right that gives you a popup titled "Add New Alignment Constraints" and click the checkboxes "Horizontal Center in Container" and "Vertical Center in Container". Then click the "Add constraints" button at the bottom of the popup.
Assuming you want the change to occur instantaneously, you should simply be able to change the frame for the image view to your desired CGRect:
imageView.frame = rect
This is assuming your UIImageView autosizes and/or has the correct aspect ratio. If not, you should adjust your CGRect accordingly. Alternatively, you can look into animating the frame property.
If you're just going to be doing this when the view initially loads, it would be much easier center the image with Auto Layout by horizontally and vertically centering it in its container, as Duncan C suggested.