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?
Related
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.
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.
Below is my storyboard, I had quite a lot of constrains to make those four buttons fit into the screen nicely. However, after I put the images into the buttons and check the storyboard preview, all images are distorted. I try to adjust (had tried all of the options) the view mode at attribute inspector panel but seems no effect at all.
One more thing, I am using PDF as vector type image set, not regular .png file I am wonder if that matters? Should I change the way vector image displayed inside UIButton programaticly?
The verctor image size was 500px*500px. I resize the image into 50px*50px after #orkenstein 's reminader, xcode may not be able to handle oversized vector image that well. The image appear inside button as it is exactly 50px*50px and lost its ability to scale. Also as he sugested I should but the vector image inside an UIImageView and after my test, it works perfectly fine in an UIImageView.
Setting the contentMode on the button itself doesn't work for me, but setting it on the button's imageView does. From this Stackoverflow answer:
button.imageView?.contentMode = .scaleAspectFit
I am using too many number of images for buttons in my project. I found some issues while doing device test. App couldn't detecting the finger tapping sometimes. I need to increase the touch sensitivity programmatically to resolve this issue.
Like apple documentation says:
Make it easy for people to interact with content and controls by giving each interactive element ample spacing. Give tappable controls a hit target of about 44 x 44 points.
Make the button tappable size at least 44x44 pixels and it should work.
More informations here
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/LayoutandAppearance.html
it's not possible, but you might have smaller action button which you could resize little more or you can have bigger transparent action button behind your smaller action button so you can get what you are looking for!
Add images to button like this -
here you can see the button border are larger that image size, so you can increase the touch area of button in this way.
For further image adjustment inside button, you can use image insets, which are also present in the same inspector in xib.
So, using Objective C, I have a button to post to Twitter. I saved the image and placed it in my supporting files. I have the button looking like this: http://i.imgur.com/z8Pswxq.png in the storyboard.
But when I go to simulate the app with iOS simulator for it ends up looking like this: http://i.imgur.com/KW3oQYg.png
It seems to revert back to the size of the actual image no matter what. I can't seem to see a way to make it the size of the button. Unless is there a way to programmatically do it? Probably missing like, a checkbox somewhere.
I'm sorry about posting the links instead of using images: have like, 1 reputation.
Presumably you don't have any explicit constraints that are dictating the size of your button. The size of your button is being dictated by the button's intrinsic content size. In this case, that content size is the image size.
Either resize your image (e.g., programmatically or in your favorites graphics program), or install explicit constraints on the button that dictate the button's size. The constraints could be installed in IB by control-dragging with zero code.