Centre a UIImageView inside a CGRect - ios

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.

Related

How to center both vertical and horizontal inside a UIStackView

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.

How to set AutoLayout constraint of UIimageView with UIButton?

This tree include the cicle is a UIImageView's image, but the text in the circle is a UIButton. When the device is changed, like iPhone5 change to iPhone6 Plus, the image with scale to big. The Button always move to the circle's outSide. How to Solve the problem?
Autolayout has a feature called propotion. It means that by selecting any 2 UI objects you can set propotion between them for any constraint such as width, height, etc.. In tour case also select imageview and button then move to the constraints view, double click on any objects width constraint i.e either button width or image width. There you can find a option called propotion. As per your requirement you want to have a button always inside the image view, means that buttom propotion should be less than the imageview propotion. So you can set 2:3, means button width is 2/3rd part of the imageview. Now button will be always part of the imageview irrespective of the device which you run.
You can Add proportional width and height constraints to your buttons relative to imageView like this screenshot
Here I have not added fixed height and width constraints to imageview, because image view takes width and height from its content i.e from image provided to it, if you want to fix height and width of image view then you can provide those too.

How to center view with autolayout

I have universal app. Subview is rectangle which is positioned inside View.
How can I keep it centered and make it automatically adopt it's width depends on screen width ?
e.g.
In order to center a view within it's superview, you need to check Horizontal Center in Container and Vertical Center In container in the Autolayout Align section:
You could also make use of Size Classes :
With size classes, a storyboard or xib file can be used for any
available screen area. You build your interface as it will look in
most sizes, then update only the parts that need to change when the
available screen size changes.
As you said you want this only in portrait means its not an adaptive layout....So its fairly simple now....
As you want the width of object resize according to screen width... Just pinned right and left edges of screen to adopt width accordingly (or use equal width)
As you not mention about height...so give fixed height to object....
And for making the object to the center of the view controller ....
Use center vertically and horizontally....and you are done..
Here is the image of what I said for better understanding

How to scale a UIImage view to a square with Autolayout inside a UIScrollView

I've been struggling to learn autolayout (finally). I want to have a vertically scrolling UIScrollView that holds all the content for the view. I want to pin a UIImage view to the top of the scrollview and keep the image a square (scaleToFill). Basically I want to do this (only I want to do it with autolayout):
I can't get the image to keep its aspect ratio while staying within the screen bounds. Whenever I add an aspect ratio constraint the imageView grows to like 600 points (the width of the png I think), but I want it to just be as wide as the screen.
I think I am setting the constraints up for the UIImageView correctly because if I get rid of the scrollView and just drop the imageViw directly on the view then it seems to do what I want it to. The trick is putting it inside the scrollView.
This is how I currently have it set up:
The 4 vertical/horizontal space constraints on the scroll view are all set to constant 0.
Since the size of the UIScrollView depends on its content, you cannot have the size of your UIImageView subview dependent on it.
You have to remove the aspect ratio constraint of the UIImageView and use a fixed width one. You can also have your UIImageView width dependent on another view in the UIScrollView which has either a fixed width or otherwise unambiguous width.
Don't forget to use Placeholder as the intrinsic size value in Interface Builder.
Hope this helps.

Autoarrange subviews in a zoomed UIView

I'm trying to zoom in and out an UIView, and rearrange it content to look similar for both states: zoomed and normal.
This picture shows the default state (the view that I'm going to zoom has orange color and has 5 UIImageViews) :
When I press "Zoom in" button I change orange view frame:
_page.frame = self.view.bounds;
And I'm getting the following result:
But the goal that I want to achieve is something similar to this (same result if I would scale the view):
It means that I must change frames for each subview, but it could be complicated when view would have many objects on it.
What I'm asking for are some hints or methods how can I get desired result without accessing subviews.
There are be hacks to do this, but the proper way would be to use auto layout. You don't have to access any subviews and will be able to do it in the storyboard/IB.
If you use auto layout, you can actually create constraints which will pin the following attributes of the subviews:
Pin the top subview's top space and leading space to the container
Pin the all but the last subviews' vertical distance to its nearest neighbour and leading space to container
Pin the last subview's top vertical space to its nearest neighbour and bottom space to container and leading space to container
Set constraints for height and width but set the priority to low
In addition to setting the frame (which just changes the size of the view) you want to change the transform (scale the view) Try something like:
_page.transform = CGAffineTransformMakeScale(2.0, 2.0)
You'll probably want to calculate the scale factor based on the old view size and the new size.

Resources