美文网首页
iOS导航按钮响应范围扩大问题

iOS导航按钮响应范围扩大问题

作者: 红姑娘 | 来源:发表于2016-10-17 09:53 被阅读131次

我们直接在leftBarItem添加一个自定义button的时候,这个button的有效点击范围将会很大,接近navigationBar的titleview。
如果我们只想要响应范围是按钮的区域的时候。可进行如下操作:

    +(instancetype)barButtonItemIcon:(NSString *)icon HighlightedIcon:(NSString *)highIcon Target:(id)target action:(SEL)action{

        UIButton *button = [[UIButton alloc] init];
        [button setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:highIcon] forState:UIControlStateHighlighted];
        button.frame = (CGRect){CGPointZero,button.currentBackgroundImage.size};
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        //先将你的按钮添加到一个同样大小的uiview中,然后再将这个uiview添加到leftBarItem里。
        UIView *view = [[UIView alloc] initWithFrame:button.frame];
        [view addSubview:button];
        return [[UIBarButtonItem alloc]initWithCustomView:view];
    }

相关文章

网友评论

      本文标题:iOS导航按钮响应范围扩大问题

      本文链接:https://www.haomeiwen.com/subject/fjvayttx.html