Resize UIImageView with autolayout - ios

I've got a view with a UIButton in it. I've got some animations that, among others, scale the button down.
The problem is that the button is actually scaling well following the constraints when it only has text, but when I set an image either as its image or its backgroundImage, the button is not scaling anymore.
I've tried playing with contentModes of both the UIButton and its inner imageView with no results (I've tried, literally, all the possible answers of this, this, this and this stackoverflow questions).
Any hint? Thank you all in advance
UPDATE
I've noticed that the issue is with UIImageViews, not UIButtons (the problem with the button was due to the imageView inside it).

I think your UIButton does resize, but the ImageView inside does not. The ImageView of the UIButton is always displaying normal image size.
The only possibility I found is to put another UIImageView below the Button with constraints top/bottom/left/right to the Button. This one can be perfectly adjusted.
I use subclassed buttons for that with their own ImageView inside. There you need to be careful to put userInteractionEnabled = false on the ImageView.

Finally I've got it.
In both cases (UIButton and UIImageView), you have to adjust Content Compression Resistance Priority to let the view to scale down. In my case, setting it to 749 was enough.

Related

Increasing size of image in UIButton

Currently, I have a UIButton and want to set an image inside of it. I'm successfully able to set the image to inside the UIButton but for some reason, the image is VERY small compared to the UIButton. I want to increase the size of the image - no, not cover the entire button just a slight increase so it's a bit more visible on the UI.
More information, my UIButton is 40x40. The answers I've seen on StackOverFlow have indicated changing the Content Mode but even those answers have not helped. I'm a little stuck at this point so any expertise/advice would be highly appreciated!
Figured it out. I had to use image insets. This property allows to reposition the drawing rectangle for the button image. By setting values to the top, bottom, left, and right image insets we're creating a rectangle and thus, increasing the space for our image to be draw in.
I had actually seen this suggestion in other answers and had tried it but on IB, nothing seemed to change but I finally decided to test it on simulator and saw the affects.

UIbutton stretch when clicked unexpectively

I am trying to modify a frame work and have encountered the following problem.
I have replaced one of the button image in the framework's xib file with my own one. The image itself is approximately 20Wx20H but I have set the button size to be 40Wx40H so that the clickable area is larger than the actual image.
When the VC is displayed, I see the image to be normal 20x20 aspect ratio. However, when I clicked on the button, it stretches to 40x20 and goes back to default upon release. I was wondering if there is a UIButton parameter that I can disable which was enabled by the framework somewhere? I have done a search on the buttons in the code and could not find anything relating to stretching the button. So I am guessing somewhere in IB there is my solution?
Below are the images for the button when touch down or not touched down.
Have you tried the following?
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
Also are you using autolayout? If yes, what are the constraints?

Changing size of UIButton while using Autolayout?

I'm pretty new to ios. I'm working on a project which is going to frequently use a certain UIView class throughout the application. This class is simply an image that does some transparency stuff with the background color. This works fine so far. This class sometimes exists as a lone UIView, and sometimes as a button subview.
When I add this UIView to a UIButton as a subview, the full contents of the UIView are displayed at full size, but the clickable area of the button remains the size defined in the xib unless Use Autolayout is turned off (even if I manually try to set the button's frame.)
I would like to put a UIButton on the xib as a placeholder, and then later define its size/clickable area based on the size of the overlay image which the UIView that was initialized with.
Should this be possible, or am I misinterpreting the usage of xib files/autolayout?
under ViewDidLoad I have...
- (void)viewDidLoad{
[theButton addSubview:_theView];
CGRect buttonFrame = theButton.frame;
buttonFrame.size = CGSizeMake(_theView.getSize.width,_theView.getSize.height);
[theButton setFrame:buttonFrame];
}
However, the frame stays the same size when I print the button info before and after I try calling setFrame.
(note that '_theView.getSize' was added by me)
Under Autolayout, views don't have frames at viewDidLoad. Try your code in viewWillAppear: or viewDidLayoutSubviews.
Under Autolayout, you don't set frames. You edit constraints instead. Setting a frame will work until the next layout pass, when your layout will revert to that described by your constraints.
To size a button to fit a subview, you can try something like this (in visual format language): |-[theView]-| but it would depend what constraints are in place from your xib.

Adjusting UIButton's imageView properties

I'm having trouble figuring out how much I can adjust the imageView property of a UIButton. In the docs, it says:
Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance and behavior of the button’s view.
I'm hoping to perform a transition where I shrink the size of the button in an animation, and I'd like the button's image to also shrink accordingly. However, I find that I have no control over the size of that image. If I modify either the frame or the bounds of UIButton's imageView property, nothing seems to happen. The image seems to want to retain it's standard dimensions. Changing autolayout properties on the imageView or contentMode properties on the button don't seem to help either.
In fact, the only thing that seems to work at all is to use UIEdgeInsetsMake to "squish" the image from all sides, but that isn't animatable.
I'd prefer not to use the backgroundImage property either, since I'm already using that to style the button in the first place.
You could add an own UIImageView as subview to the UIButton instead of using the "embedded" imageView.
You can set the frame of "your" image view as usually relative to the surrounding UIButton. You will have full control on it's position and you will also be able to animate it.

imageView in UITableView cell violates setClipsToBounds on touchdown event

I have a UITableView whose cells have an image from a URL placed in its imageView.
The image isn't square (is landscape), however it initially loads as a square in the manner in which contentMode is scaleAspectFill and setClipsToBounds is YES. So far so good.
However, when I touch down on the cell, the image "widens", that is, it retains the scaleAspect part, but now bleeds out to the right as if setClipsToBounds is NO.
The same behavior occurs if the row/cell goes out of view and comes back in recycled, in which case I have the image obtained from an in-memory cache rather than from the URL.
I've tried explicitly setting both contentMode and setClipsToBounds for the cell.imageView, but it seems to have no effect. Any advise? Thanks.
Before touchdown:
After touchdown (or recycle from cache):
imageView cell property (same as other textLabel and detailTextLabel) changes its frame in layoutSubviews accordingly to image size. So you can reimplement layoutSubviews to have fixed frame rect. BTW, try TableKit library.
Heres a link to the correct answer if anyone is still looking for a solution: UIImageView in UITableViewCell changes size on touchDown
Its because you named it imageView, name it something like image_View and the problem is solved!

Resources