Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase selection area of UIButton?

I have made the UIButton programmatically

togglebutton = [UIButton buttonWithType:UIButtonTypeCustom]; togglebutton.frame = CGRectMake(42, 15, 80, 21); [togglebutton addTarget:self action:@selector(toggleview)    forControlEvents:UIControlEventTouchUpInside]; [togglebutton setImage:[UIImage imageNamed:@"squ.png"] forState:UIControlStateNormal]; [buttonView addSubview:togglebutton]; 

enter image description here

It is looked as the right button in above image. Now the requirement is that the selection area of this button should be more than then the uibutton image. So that the user will be able to click the button easily by touching on near by area of particular button.

[togglebutton setImageEdgeInsets: UIEdgeInsetsMake( 0, -30, 0, -25)]; 

I tried to set the image inset but it making the image irregular. Please look upon this issue.

like image 809
Minkle Garg Avatar asked Jun 22 '13 09:06

Minkle Garg


People also ask

How do I change text on UIButton?

Programmatically. To make a multi-line text in UIButton, you insert a new line character ( \n ) wherever you want in button title and set lineBreakMode to byWordWrapping . You can adjust text alignment with . textAlignment .


2 Answers

You can achieve this by setting the button's contentEdgeInsets, for example:

toggleButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0); //set as you require  
like image 55
Nitin Gohel Avatar answered Sep 30 '22 08:09

Nitin Gohel


For Swift

You can override func 'pointInside' of UIView class to expand clickable area.

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {     // padding : local variable of your custom UIView, CGFloat     // you can change clickable area dynamically by setting this value.      let newBound = CGRect(         x: self.bounds.origin.x - padding,         y: self.bounds.origin.y - padding,         width: self.bounds.width + 2 * padding,         height: self.bounds.height + 2 * padding     )     return CGRectContainsPoint(newBound, point) } 

Use like this,

class MyButton: UIButton {     var padding = CGFloat(0) // default value      //func pointInside at here!! } 

When you init your button, set your custom padding.

func initButton() {     let button = MyButton(frame: CGRect(x: 100, y: 100, width: 30, height: 30))     button.padding = 10 // any value you want     view.addSubview(button) } 

then your button is in frame (x: 100, y:100, width: 30, height: 30)

and your button's clickable area might be in the frame with (x: 90, y: 90, width: 50, height: 50)

** Be sure to check overlapping with any other view, because its clickable area is larger than it looks.

Update for Swift 3

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {     let padding = CGFloat(10)     let extendedBounds = bounds.insetBy(dx: -padding, dy: -padding)     return extendedBounds.contains(point) } 
like image 39
Mark Avatar answered Sep 30 '22 09:09

Mark