I have a UIImageView which is set to mode Aspect Fit. Now I want to place an edit button on top right corner of that image view. I am using auto layout and have a constraint of (Button->Trailing Space to superview) and the layout is connected via IBOutlet.
Once the image is set in UIImageView, I want to resize the ImageView Frame, so I can place the UIButton on top right. But UIImageView is not resizing as per image. Look at the gray part in the attached image (It is the background of ImageView)
imageView.clipsToBounds = YES;
imageView.image = image;
[imageView sizeToFit];
buttonLeadingSpace.constant = imageView.frame.origin.x+imageView.frame.size.width-44;
First of all, you should align your button with ImageView top & trailing. you can add constant=padding you need for your button.
UIImage is displaying as per your properties set for UIImageView (ie. contentMode = AspectFit). Here are the few options you might be interested in
If you want to fill the complete width just remove the height constraint from the UIImageView. It will fill the width and increase the UIImageView height as per image aspect ratio.
If you want same height and width of UIImageView set contentMode = aspectFit. this will fill the complete UIImageView but might clip the image.
Remove leading and trailing constraints from UIImageView. Align horizontal center and add image width constraint.
Related
I am having an imageView and a label side by side. I want to remove width and height of the image when it's hidden. Yes we can do it programmatically by setting the height and width constraints to 0, but is it any other way by storyboard. I have placed imageView inside horizontal stackView without width constraints but it is working just opposite, increasing it's width on hiding the imageView.
How about use another UI for shrinking ImageView
for example
AS IS
UIStackView with arranged UILabel, UIImageView, UILabel, UIView(isHidden true)
when you need to shrink UIImageView with hidden then make hidden UIImageView and make shown UIView.
It makes UIImageView width 0 like because UIView should be expanded full size (hugging priority less)
TO BE
UIStackview with UILabel, UILabel, stretched UIView(low hugging priority and isHidden false)
I'm having issues centering my UIImageView in a UITableViewCell.
The image below you can see it's not correctly aligned. My cell is setup like so, with the image view pinned to the top, bottom and sides of the cell, centered horizontally and aspect ratio on.
Any ideas what I might be doing wrong here?
UPDATE: Curiously if I tap on the cell and select it the image then centers correctly.
Try
imageView.contentMode = UIViewContentModeCenter;
//Or
imageView.contentMode = UIViewContentMode.ScaleAspectFit;
Hope it will solve your problem.
Try to remove leading and trailing constraints and add Center Horizontally constraint from your imageView to the label which is above that imageView.
Please add following 2 constraints and image will starts appearing in centre of parent view.
1) Centre horizontally in parent
2) Centre vertically in parent
I have a view named myView which is always half the screen and sit at the top of screen.
Inside this view I added an UIImageView with 120x120 size which sits in center of the myView (horizontally and vertically). Inside the IB, to satisfy the constraints(X & Y) I always need to set a fixed width and height for my image, after I set center horizontally and center vertically.
But with a fixed height and width, the image doesn't resize when changing the screen size. I want my image to resize when running on iPhone5 or iPhone 4s, because myView will resize.
I need something like the image should depend on the myView size.
How to actually achieve this ?
set imageView.clipToBounds = yes, and also set imageView.contentMode = UIViewContentModeScaleAspectFit;
Update
You are talking about imageView not the image, in that case it will not resize due to constant width and height, do one thing, create IBoutLet of constraint and change there values when required, or you can also set Aspect ratio with superView
If you set the image with fixed width and height it will not change respectively.
You can either set it to be relative to myView or you can instead set leading and trailing size from all for edges and delete the center constrains.
Your constraints should look like this:
For Swift 4: Updating answer of #Adnan Aftab
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFit
I have a UIScrollView with a single UIImageView child. The scrollView is pinned on all sides to the (root) parent container with autolayout, and the child imageview is also pinned to all sides with content mode set to AspectFill.
UIImage *image = [UIImage imageNamed:#"photo1.jpeg"];
_imageView.image = image;
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_scrollView.contentOffset = CGPointZero;
_scrollView.contentSize = _imageView.image.size;
_scrollView.zoomScale = 1;
The image in this case is wider than my screen. When I launch my app, the imageview is correctly displaying the image filling up the screen. However, I can't seem to pan the image left or right. When i zoom in, I can pan, however, I'm unable to pan to the corners of the image.
I feel like my content size is not being computed properly thanks to auto-layout, but I'm not sure which parameters I can play which would allow me to scroll the image to its edges.
I've attached an image below, where you can see that the image can't be scrolled in a way that displays the beginning of 'happiness'. I also uploaded the sample here
the child imageview is also pinned to all sides
Instead, turn off auto layout for the child image view (set its translates... to YES). You are already setting the scroll view's contentSize to the image size, so now scrollability will leap into life and will interface correctly with zooming.
My UIView (width: 352px) has a UIImageView subview (default width in Storyboard: 312px).
I want the UIImageView to adapt itself to the dimensions of the image it contains with the constraint that the width of the image view shouldn't exceed a maximal width size (in my case, 312px).
I set up the 'autosizing' configuration of my UIImageView to have a fixed left, top and right margin size. Nevertheless, when I call sizeToFit on my UIImageView and its image is larger than 352px, the UIImageView gets wider than its containing UIView.
Is there a convenient method to prevent such a behavior without doing the math based on the image dimensions? Am I using sizeToFit the right way?
Just use setClipsToBounds:
[imageView setClipsToBounds:YES];