扩大按钮点击范围

点击某个按钮弹出一个遮罩视图(黑色,透明度0.4),然后在遮罩视图上添加了一个从视图底部动画弹出的视图,弹出视图上有个按钮,点击可以移除遮罩视图和该视图,但是貌似该按钮有点小,不好点,然后我就用UIButton的title偏移量使得按钮的点击范围较大但title文字的位置和大小仍和之前显示一致。 注:布局用的PureLayout这个第三方库。 代码:

//
//  ViewController.m
//  CaiTest-luckyCoderCai
//
//  Created by luckyCoderCai on 16/8/3.
//  Copyright © 2016年 luckyCoderCai. All rights reserved.
//

#import "ViewController.h"

#import "PureLayout/PureLayout.h"

@interface ViewController ()

@property (nonatomic, strong) UIButton *testBtn;
@property (nonatomic, strong) UIButton *testButton;

@end

@implementation ViewController

- (UIButton *)testBtn
{
    if (!_testBtn) {
        _testBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _testBtn.backgroundColor = [UIColor purpleColor];
        _testBtn.translatesAutoresizingMaskIntoConstraints = NO;
        [_testBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
        [_testBtn addTarget:self action:@selector(testBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _testBtn;
}

- (UIButton *)testButton
{
    if (!_testButton) {
        _testButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _testButton.backgroundColor = [UIColor purpleColor];
        _testButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_testButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
        [_testButton addTarget:self action:@selector(testButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _testButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    [self createUI];
    
}

- (void)createUI
{
    [self.view addSubview:self.testBtn];
    [self.testBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:15];
    [self.testBtn autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:100];
    [self.testBtn autoSetDimensionsToSize:CGSizeMake(16, 16)];
    
    [self.view addSubview:self.testButton];
    [self.testButton autoPinEdgeToSuperviewEdge:ALEdgeLeading];
    [self.testButton autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:150];
    [self.testButton autoSetDimensionsToSize:CGSizeMake(31 + 15, 31)];
    [self.testButton setImageEdgeInsets:UIEdgeInsetsMake(15/2.0, 15, 15/2.0, 15)];
}

#pragma mark -
- (void)testBtnAction
{
    NSLog(@"---testBtn");
}

- (void)testButtonAction
{
    NSLog(@"---testButton");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

复制代码

图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值