How to increase tapable (hitting) area of (custom Type) UIButton without increasing size of background image

前端 未结 3 630
走了就别回头了
走了就别回头了 2020-12-30 08:00

is it possible to increase tapable area of UIButton without changing size of Button\'s background Image

I tried:

[shareButton setContentEdgeInsets:UI         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 08:30

    Use the design approach you like (Interface Builder / Visual Format Language) together with Autolayout and layout the UIButton with the required size. Set title or image as content and use a transparent image with the size of the tapable area as background image.

    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setImage:[UIImage imageNamed:@"contentImage"] forState:UIControlStateNormal];
    [_button setBackgroundImage:[UIImage imageNamed:@"transparentImage"] forState:UIControlStateNormal];
    

    Here a example with _button.layer.borderWidth = 1.

提交回复
热议问题