UIImageView not centered in cell - ios

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

Related

Constrain problem involving UICollectionViewCell and subviews defies logic

I have a collectionviewcell with a button and a label like this:
The button is a 100x100 square, the label is a 100x40 rectangle. The cell is adjusted to 120x140.
So the constraints I have added are:
Button:
aspect ratio 1:1
top to superview space 0
bottom to label space 0
center horizontally in respect with cell
width = 100
LABEL
same width as button
center horizontally in respect to button
height 40
top space to button 0
bottom space to cell 0
I see constrain errors everywhere.
I have tried to embed these two views on a stack. It craps everything.
Then I have tried to embed the stack into a view, same problem.
This defies any logic.
Can you guys tell me how in the name of heaven I constrain these elements? All I want is both elements centered horizontally, the button on top, the label on the bottom, both with the same width of 100, the button squared and the label with a hight of 40. Both in a cell of 120x140.
Thanks
This is how you should set your constraints using a stack view in a cell to accomplish what you need: (cell width = 120, height = 140)

Change Constraints constant on view load has no effect

I have UIPageViewController that contains simple UIViewControllers. The View of the VC contains an UIImageView (aspect fit) at the top and a UITextView at the bottom.
The LayoutConstraints for the ImageView are:
Fixed Width: Which has an Outlet to the VCs-Class
Fixed Height: Which has an Outlet to the VCs-Class
Center X is equal to the superviews centerX
Top is equal to Superviews MarinTop (+8)
The LayoutConstraints for the ImageView are:
Top is equal to ImageView bottom
Bottom is equal to superviews MarginBottom (+8)
Width is equal superviews width (Multiplicator is 0.8)
This works as expected.
But when the VC is loaded and has the image to display, I want to change the size of the imageView. There is a minimum and a maximum size and if a image view is smaller or larger it needs to be resized. Otherwise the the imageview has to get the size of the image.
So I calculate the size for the actual image and set the constant for the two LayoutConstraints. The values are correct but the ImageView does not change.
I tried to set them in viewWillLayoutSubviews but no change.
What am I doing wrong?
Try update your views inside viewDidLayoutSubviews:
- (void) viewDidLayoutSubviews {
// setup your constraints here
}
Hope this helps.
Write your change constraint code in the below provided block
dispatch_async(dispatch_get_main_queue(), {
})
Try changing the vertical spacing between the ImageView and the textfield to be flexible (>=0). If that doesn't solve the issue, add:
[view setNeedsLayout];
[view layoutIfNeeded];
After you adjust the size of the imageview to size of the new image.

How to resize a fixed width and height UIImageView with auto layout

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

UIScrollView Zoom: Limit panning to imageView.image bounds

I have a screen-sized UIScrollView holding an identically sized UIImageView.
I have an image set inside of the UIImageView with AspectFit, so with a wider image, there are black bars on the top and bottom. This is as expected when zoomed out.
Double tapping the screen forces a [self.scrollView setZoomScale:self.scrollView.maximumZoomScale animated:YES]; The problem with this is that on images like the one mentioned above, I'm able to pan far beyond the edges of the photo.
I assume this is because I return imageView; in (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView and the imageView height is taller than the image, so I'm able to pan to the top and bottom of the imageView, ignoring its contents.
So, how do I limit panning the scrollview to the bounds of the image?
U need to use this: Emulating aspect-fit behaviour using AutoLayout constraints in Xcode 6
Aspect fit on Autolayout. Then You need add aspect ration constraint when loading image, if images are different size. U can get one error, image after zoom sticks to right edge. Still can't solve it.
Just set the content size of scrollview to the size of image just by this code:
scrollView.contentSize = [scrollView imageView].image.size;
Note that you should do this at the time you set image for your scrollview imageView.

Placing the button on top right of UIImageView

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.

Resources