AutoLayout button with image is resized to actual image size - ios

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.

Related

Prevent resize of UIViewController when changing orientation

The Maps app has a "custom" behaviour and doesn't resize the bottom menu when in landscape orientation.
What's the best way to achieve the same effect, short of resizing the view manually on every orientation change?
See attached images to get a better understanding.
This is done using size classes.
The easiest way to do it is to use the size classes right from your storyboard.
Right next to constraints and size related options from the size inspector you have this little + sign.
Hit it to define a different behavior for each size class.
See here for more, including how to use size classes from your code.

Xamarin iOS - loading a button image from code

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

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?

interface builder : make the view the same size as the iphone, to know exactly what constraints to add?

is there a way to have the view from interface builder in Xcode, be the same size as the iphone we use to test the app? The size of the viewcontroller seems to be 600*600. So when I move a UILabel somewhere to the left for example, it will be placed as if it was on the right on my iphone. Which is not easy to set the right constraints.
Do you have some tips to manipulate the items and place them easily? For now, I just align the item with the center point, and move it little by little.
The reason this happens is because of Apple's emphasis of size classes. It looks like a rectangle because whenever you are adding constraints and views, they'll be applied to every size class (i.e. horizontal/compact, vertical/compact, etc).
In english, it means it's applying those changes to run as such on any device.
To create constraints and views for just one size, you'll want to edit the size class you are working with. At the bottom of Xcode where it says "wAny hAny", click that and you can set what device you want to work with:
The whole idea of this is adaptive design, and I've got a post explaining it in more detail here.
The thing is that there is no one correct size for the iphone screen because there are so many different screen sizes. You can set the view size to anything you want in interface builder by clicking on the view and going to simulated metrics -> size but what you really want is autolayout so that your views fit properly on every device.

Increase touch sensitivity of iPhone app programmatically

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.

Resources