美文网首页
快速创建UIBarButtonItem

快速创建UIBarButtonItem

作者: CodeGeass | 来源:发表于2018-01-10 11:26 被阅读18次
    #import <UIKit/UIKit.h>
    
    @interface UIBarButtonItem (ZDExtension)
    
    /**
     font=16,白色
     */
    + (instancetype)itemWithTitle:(NSString *)title
                           target:(id)target
                           action:(SEL)action;
    
    /**
     可自定义title的富文本
     */
    + (instancetype)itemWithTitle:(NSString *)title
                       attributes:(NSDictionary *)attributes
                           target:(id)target
                           action:(SEL)action;
    
    
    /**
     使用initWithCustomView
     */
    + (instancetype)itemWithImage:(NSString *)image
                           target:(id)target
                           action:(SEL)action;
    
    + (instancetype)itemWithImage:(NSString *)image
                 highlightedImage:(NSString *)highlightedImage
                           target:(id)target
                           action:(SEL)action;
    
    @end
    
    #import "UIBarButtonItem+ZDExtension.h"
    
    @implementation UIBarButtonItem (ZDExtension)
    
    + (instancetype)itemWithTitle:(NSString *)title
                           target:(id)target
                           action:(SEL)action
    {
        return [self itemWithTitle:title
                        attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16],
                                     NSForegroundColorAttributeName : [UIColor whiteColor]}
                            target:target
                            action:action];
    }
    
    + (instancetype)itemWithTitle:(NSString *)title
                       attributes:(NSDictionary *)attributes
                           target:(id)target
                           action:(SEL)action
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:attributes[NSForegroundColorAttributeName] forState:UIControlStateNormal];
        button.titleLabel.font = attributes[NSFontAttributeName];
        [button sizeToFit];
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        return [[self alloc] initWithCustomView:button];
    }
    
    + (instancetype)itemWithImage:(NSString *)image
                           target:(id)target
                           action:(SEL)action
    {
        return [self itemWithImage:image
                  highlightedImage:nil
                            target:target
                            action:action];
    }
    
    + (instancetype)itemWithImage:(NSString *)image
                 highlightedImage:(NSString *)highlightedImage
                           target:(id)target
                           action:(SEL)action
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:highlightedImage] forState:UIControlStateHighlighted];
        [button sizeToFit];
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        return [[self alloc] initWithCustomView:button];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:快速创建UIBarButtonItem

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