IPhone: How to make UIButton, which you can easily click?

I am customizing the navigation bar using UIView and UIButtons instead of UINavigationBar .

But my UIButtons in the navigation bar does not react sensitively. A.

I need to touch almost the center of UIButton to click.
It does not respond if I touch the edge of UIButton .

But the buttons on a regular UINavigationBar can be tapped by clicking on the edge of the button.
Even by pressing a button, it can be pressed.

The shutter button or Option button in the camera application can also be activated by touching the edge or outside the buttons.

How can I implement these easily customizable buttons in my application?

+7
source share
3 answers

Use the image and create a custom button. Set the button so that the image does not scale to the size of the button view, but instead, it’s just Center. Expand the size of the button so that it is larger than the image on each side. Apple also does this with buttons such as tab buttons.

+11
source

UIButton has an imageEdgeInsets property specifically for this purpose. Just make the UIButton frame as large as you need for the touch area, and don't scale the image inside it appropriately using imageEdgeInsets .

+10
source

Disclaimer: This code has not been tested, but it gives you an idea of ​​how this can be done.

You make a button (in this case 40px x 40px), and then add a background image that is smaller, which makes it appear that the image is very “accessible”.

 // This image is 20px x 20px (Just an example) UIImage* backgroundImage = UIImage imageNamed:@"backgroundImage.png"] // Custom button, remember to add a target method UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom]; customButton.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); customButton.contentMode = UIViewContentModeCenter; [customButton setImage:backgroundImage forState:UIControlStateNormal]; UIBarButtonItem* customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton]; self.navigationItem.rightBarButtonItem = customBarButtonItem; [customBarButtonItem release]; 
+4
source

All Articles