如何增加UIButton的选择范围?

时间:2013-06-22 09:13:32

标签: ios objective-c uibutton

我已经以编程方式UIButton

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

在上图中看起来是右键。现在的要求是该按钮的选择区域应该大于uibutton图像。这样用户就可以通过触摸特定按钮的区域来轻松点击按钮。

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

我尝试设置image inset,但它设置为image irregular。请看这个问题。

9 个答案:

答案 0 :(得分:46)

您可以通过设置按钮contentEdgeInsets来实现此目的,例如:

toggleButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0); //set as you require 

答案 1 :(得分:17)

对于Swift

你可以覆盖func' pointInside' UIView类扩展可点击区域。

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)
}

像这样使用,

class MyButton: UIButton {
    var padding = CGFloat(0) // default value

    //func pointInside at here!!
}

初始化按钮时,请设置自定义填充。

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)
}

然后你的按钮在框架中(x:100,y:100,宽度:30,高度:30)

并且您按钮的可点击区域可能位于框架中(x:90,y:90,宽度:50,高度:50)

**请务必检查与任何其他视图的重叠,因为其可点击区域大于其外观。

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)
}

答案 2 :(得分:3)

尝试使用UIButtoncontentEdgeInsets属性。

答案 3 :(得分:3)

在xcode 9中,您可以执行以下操作:

xcode 9 screenshot

这意味着您必须补偿图像偏移和内容偏移以确保居中的图像定位。

答案 4 :(得分:2)

下面的代码解决了我的问题。这是非常基本的方式, 试试这个:

    button.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 10, 10);  //Change x,y,width,height as per your requirement.

答案 5 :(得分:1)

目标C

创建此自定义UIButton类并将其分配给您的按钮。然后从viewController中设置这些属性值。

//This is .h file
     #import <UIKit/UIKit.h>
        @interface CustomButton : UIButton
        @property (nonatomic) CGFloat leftArea;
        @property (nonatomic) CGFloat rightArea;
        @property (nonatomic) CGFloat topArea;
        @property (nonatomic) CGFloat bottomArea;
        @end



        //.m file is following
        #import "CustomButton.h
        @implementation CustomButton

        -(BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
        {
            CGRect newArea = CGRectMake(self.bounds.origin.x - _leftArea, self.bounds.origin.y - _topArea, self.bounds.size.width + _leftArea + _rightArea, self.bounds.size.height + _bottomArea + _topArea);

            return CGRectContainsPoint(newArea, point);
        }
        @end

答案 6 :(得分:0)

在我的情况下,我的按钮图像很小,我想增加按钮区域而不增加uibutton图像尺寸,因为它看起来很模糊。所以我增加了按钮框,然后设置按钮图像setImageEdgeInsetsedgeInsets的正值将缩小,负值将会扩大。

self.menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.menuButton addTarget:self
                  action:@selector(onSideBarButtonTapped:)
        forControlEvents:UIControlEventTouchDown];
[self.menuButton setImage:[UIImage imageNamed:@"menu"]
                         forState:UIControlStateNormal];
self.menuButton.frame = CGRectMake(10, 3, 40, 40);

现在我要缩小图像尺寸,使按钮看起来不会模糊。

[self.menuButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];

答案 7 :(得分:-2)

我会围绕该按钮创建一个UIView,以便您可以将大小设置为您想要的任何大小。然后我将UIView的背景颜色设置为清晰的颜色(不要将不透明度设置为0或不会识别手势)

然后在uiview中添加一个UIGestureRecognizer来处理相同的IBAction

当然,很多人会鼓励/更喜欢使用edgeInsets

答案 8 :(得分:-2)

以下是增加自定义UIButton的可触摸区域的示例:

UIImage *ButtonImage=[UIImage imageNamed:@"myimage.png"];
UIButton *myButton=[UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame=CGRectMake(10, 25, ButtonImage.size.width/2+10, ButtonImage.size.height/2+10);
[myButton setImage:ButtonImage forState:UIControlStateNormal];
myButton.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
[myButton addTarget:self action:@selector(crossButtonClick:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:myButton];

希望它会有所帮助。