Resizing UIImageView inside a UITableViewCell - ios

in my app the UITableViewCell size are dynamic which means each has it's own height. inside of each cell there is a background image in a UIImageView. Through storyboard, i used autolayouts to customize the UIImageView to automatically stretch as the cell is stretched. The problem is that when they stretch the whole image stretches with the corners. So i was looking up online and i came over using resizableImageWithCapInsets: in order to stretch the stretchable sides and exclude the unwanted ones. So i tried the following code in tableView: cellForRowAtIndexPath: as i wanted the image to be stretched vertically only (height):
[cell.backgroundImage.image resizableImageWithCapInsets:UIEdgeInsetsMake(16, 0, 16, 0)];
however, the problem persisted as in the picture below
As you can see, the image corners are still stretched. What am i doing wrong? is the edgeInset values wrong? or should i place the code somewhere else ?

You create UIEdgeInsets values using the UIEdgeInsetsMake() function. UIEdgeInsets encapsulates four different CGFloat values for the inset values of the top, bottom, left, and right individually. Positive inset values shrink the content area, while negative inset values will effectively increase it. Make sure you create the image first and apply the insets there and then, Once that is done then apply the image to the backgroundView as opposed to trying to edit the edge insets from the backgroundView.image property.
UIImageView *cellBGIV =
[[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"imageName"]
resizableImageWithCapInsets:UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0)]];
cell.backgroundView = cellBGIV;

Related

image in UIImageView sticks to the bottom of the UIImage space

I'm having and issue and i would like to know if doing this is possible.
I have a UIImageView with the content mode of .aspectFit, the green part is the UIImageView background color and the image inside is the actual image, i want to know if it's possible to make the image inside the UIImageView sticks to the bottom and not just float in the center of the UIImageView space.
Note: Depending on a parameter i'll have to change the UIImageView height dynamically to make the items look bigger or smaller
The photo below represents the problem description.
For your requirement, you may want to change your UIImageView's Content Mode to Bottom,
imageView.contentMode = .bottom
Put your UIImageView to UIView and then anchor image view (with content mode aspect fit) to the bottom of its superview. Also set height of ImageView equal to height of View in certain ratio
Then if you need to change height of ImageView, just change aspect ratio of heights.
did you try setting UIEdgeInsets ?
UIImageView.image = UIImage(yourImage.withAlignmentRectInsets(UIEdgeInsets(top: -10, left: -10, bottom: 0, right: -10)

UIImageView doesn't resize itself to the UIImage it holds

I have a UIImageView with Auto-layout to the container margins. I set a UIImage to it in the ViewController. If I use UIViewContentModeScaleAspectFit then the image is centred in the middle of the screen as I wanted and everything looks great, but when I give the UIImageView a background color, I can see it still spreads all the way to the container margins, and doesn't get the image's proportions and dimensions. This is my code:
UIImage *passedImage = [UIImage imageNamed:self.photoTitle];
CGRect imageBounds = CGRectMake(0, 0, passedImage.size.width, passedImage.size.height);
[self.imageView setImage:passedImage];
self.imageView.bounds = imageBounds;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
I have tried everything to fix it and looked everywhere for answers, please help me, you are my only hope.
Instead of pinning the image view's edges to the container margins, pin its center X and center Y to the container's center X and center Y.
This gives the same visual result — the image is centered — but leaves the image view free to resize itself according to its contents.
UIImageView does not resize itself according to image size it renders. If you need to size it accordingly, you need to do it yourself. Matt's answer is reasonable, but you still need to update your image view size at some point. I'd suggest doing it in layoutSubviews or updateConstraints method of your view or in view controller's viewDidLayoutSubviews.

How to prevent UITableViewCell backgroundView from stretching

I'm attempting to add a background image to a UITableViewCell so that each cell can have some spacing at the bottom.
I'm setting the cell's backgroundView property like this:
[cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"cell"]]];
The "cell" image is 20px taller than the actual heigh of the UITableViewCell, this is on purpose so that my cells can have some spacing at the bottom. Only problem is, the "cell" image will get stretched all the way down the cell's contentView.
Is there a way for me to prevent the cell from stretching the backgroundView all the way down?
Thanks in advance!
Look into the contentMode property of UIView. If the height of your background image is smaller than the height of the cell and you want the image to "stick to the top" try setting
cell.backgroundView.contentMode = UIViewContentModeTop;
Look at the "Content Modes" section on this page for more information. It mentions...
"By default, the contentMode property for most views is set to UIViewContentModeScaleToFill, which causes the view’s contents to be scaled to fit the new frame size."
Two options:
1) Make your image bigger manually, filling it with transparent space in the gaps. (Easier solution).
2) Add another UIView inside your UITableViewCell, make it as big as the image. Add your text label (if any) inside this view.

How do I draw resizable UIImage in code like this?

I want to use custom background image for UISegmentedControl like "Find My Friends". Here's how their resizable images look like:
source http://feedzr.com/source.png
source http://feedzr.com/inwork.png
How do I create that complex shadow effect in Core Graphics?
EDIT
The bottom image is just how the above looks like when working with a leather background in a real UISegmentedControl.There're lot's of effects in the resizable image like:
bottom gloss, top inner shadow and partial gradient from top to bottom.
I just can't see how & what effects are being used in this image. I'm not asking how to use UIEdgeInsets.
These are just 2 images. One with the shadow one without…
You have to create these Images with CapInsets to make them resizable
For example
UIImage *buttonImage = [[UIImage imageNamed:#"yourImage"]
resizableImageWithCapInsets:UIEdgeInsetsMake(1, 11, 0, 20)];
To know how UIEdgeInsets work read:
How does UIEdgeInsetsMake work?
Or in the Apple Doc:
UIEdgeInsetsMake
Creates an edge inset for a button or view.
UIEdgeInsets UIEdgeInsetsMake (
CGFloat top,
CGFloat left,
CGFloat bottom,
CGFloat right
);
Parameters
top
The inset at the top of an object.
left
The inset on the left of an object
bottom
The inset on the bottom of an object.
right
The inset on the right of an object.
Return Value
An inset for a button or view
Discussion
An inset is a margin around the drawing rectangle where each side (left, right, top, and bottom) can have a different value.
Availability
Available in iOS 2.0 and later.
See Also
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html

UIImageView overflowing its containing UIView after calling sizeToFit

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];

Resources