I have the following Image Set defined in my Media.xcassets library...
I am loading this image to a button as follows...
public override void ViewDidLoad()
{
base.ViewDidLoad();
FSDirectButton.SetImage(UIImage.FromBundle("FSDirectButton"), UIControlState.Normal);
}
However, the button is not the correct size. How do I load an image to a button and then have the button resize to fit the image? I am not sure which image is being picked (1x, 2x, 3x).
Here is a recipe that I have used in the past that has helped greatly: https://developer.xamarin.com/recipes/ios/standard_controls/buttons/use_an_image_for_a_button/
a few things you may also want to consider trying:
1. Make sure constraints are set so that the button is always the right size
2. If all else fails try adding the images to your resource folder and accessing them as files instead as a temporary workaround.
3. also, you may want to try SetBackgroundImage(), instead of SetImage to see if it yields different results.
4. Try setting the image for all the control states of the button (highlighted, disabled, etc.)
5. try changing the content mode for the imageview inside the button. It may just be that the image is too large and doesn't scale to fit the alloted space. access this by using FSDirectButton.ImageView.ContentMode
To answer your question about which image size is selected (1x, 2x, etc.), I believe that is determined by the resolution of the IOS device that the image is being displayed on. that way if someone uses an iphone or an iPad the image will still look nice and clear without you having to write a lot more code to choose the best image for each device.
How do I load an image to a button and then have the button resize to
fit the image?
If you want your button auto resizing to fit its image. Please use Autolayout to layout your button.
Try to add the top constraint and left constraint to define the element's position in X and Y. Then the button will auto adjust its size depending on its content if we don't add the width and height constraint to the button.
I am not sure which image is being picked (1x, 2x, 3x).
After adding the images, the system will decide which image should be picked depending on which device the app run on. we just need to use the image name without the suffix. More details about which device picks which type of image you can refer to this
Related
in Xcode interface builder, i used the highest resolution from https://github.com/hampusborgos/country-flags , 1000 pixels but the image is too big when i assign it to the button. I want to create a game for all country flags and tried figuring out a couple hours how to fit image within the button but to no avail.
Anyone can help how xcode, interface builder, can auto-resize to fit within the button size? attached 3 screenshots below
https://drive.google.com/file/d/1WvMzrkVw172Tou25m9nILbqffAZfSu6L/view?usp=sharing
https://drive.google.com/file/d/1vfaLfI8nKl4Bmk_1MMWYXX5fWpheMiZe/view?usp=sharing
https://drive.google.com/file/d/1m2Zq0xRoGPvO59ZXlDHvTya1cHcs0iji/view?usp=sharing
flagButton.imageView?.contentMode = .scaleAspectFit
just change the content mode of the button's imageView
just write this code in viewWillAppear or viewDidLoad
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?
I have added a table view, and I am display image in the cells. I have also added this code:
So that the cells resize depending on the image.
When I launch my app though, I get this :
[![enter image description here][1]]
And the images do not load untill I start scrolling...If I scroll down half the page then go back to the top, I get this: Which is correct
[![enter image description here][2]][2]
Any ideas? I have researched on google and tried the odd solution for the older versions of Xcode, But nothing seems to work!
If the image is already downloaded and resident in your cache when you pass the post to the cell them the image view will have an image and therefore an intrinsic content size. This all allows the table view to calculate the required layout.
If the image needs to be downloaded then the image view won't have an image and will size to zero because there is no intrinsic content size other than that.
If your images are all the same size then you can supply a placeholder image while waiting for the download and the cell will size appropriately.
Note also that you aren't updating the cell when the image has downloaded. When you do this you do need to check that the cell hasn't been reused (or you'll be showing the wrong image). If you don't have a placeholder or can't guarantee the image size then you will also need to ask the table view to update.
Maybe your post.downloadImage() and post.fetchLikes() should callback, and in callback, should reload cell in mainThread. hope it help.
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
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.