美文网首页
UIBarButtonItem + Extentsion

UIBarButtonItem + Extentsion

作者: geekAppke | 来源:发表于2017-06-06 11:36 被阅读9次
    • 图片,不要渲染
    • 文字,设置属性
    /**
     *  快速创建一个显示图片的UIBarButtonItem
     */
    + (instancetype)itemWithTarget:(id)target action:(SEL)action icon:(NSString *)icon highIcon:(NSString *)highIcon
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
        [button setBackgroundImage:[[UIImage imageNamed:highIcon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateHighlighted];
        // 设置按钮尺寸
        button.bounds = (CGRect){CGPointZero, button.currentBackgroundImage.size};
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        
        return [[self alloc] initWithCustomView:button];
    }
    
    • 注意:一定要把target传过去,否者self是UIBarButtonItem,找不着执行方法,会崩的
    • [[UIBarButtonItem alloc] initWithTitle:@"写私信" style:UIBarButtonItemStyleDone target:nil action:nil];

    相关文章

      网友评论

          本文标题:UIBarButtonItem + Extentsion

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