美文网首页
iOS 封装一下ButtonItem

iOS 封装一下ButtonItem

作者: 冯娇王 | 来源:发表于2021-05-27 18:15 被阅读0次
    在实际开发中,UIBarButtonItem使用是比较多的,也经常需要自定义
    //第一步继承系统的
    @interface PRBarButtonItem : UIBarButtonItem
    
    //在.m文件中咱们来实现自定义
    #import "PRBarButtonItem.h"
    @property (nonatomic, copy) dispatch_block_t callback;
    
    
    //使用类方法
    + (PRBarButtonItem *)getWithTitle:(NSString *)title
                             callback:(dispatch_block_t)callback {
        return [[PRBarButtonItem alloc] initWithTitle:title callback:callback];
    }
    + (PRBarButtonItem *)getWithImage:(UIImage *)normal
                             callback:(dispatch_block_t)callback {
        return [[PRBarButtonItem alloc] initWithImage:normal callback:callback];
    }
    
    - (instancetype)initWithTitle:(NSString *)title
                         callback:(dispatch_block_t)callback {
        PRBarButtonItem *item = [super initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)];
        item.callback = callback;
        [item setTitleTextAttributes:@{NSFontAttributeName:SLGetMediumFontWithSize(17),NSForegroundColorAttributeName:SLTextColor_000000_80()} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSFontAttributeName:SLGetMediumFontWithSize(17),NSForegroundColorAttributeName:SLTextColor_000000_80()} forState:UIControlStateHighlighted];
        [item setTitleTextAttributes:@{NSFontAttributeName:SLGetMediumFontWithSize(17),NSForegroundColorAttributeName:SLTextColor_000000_40()} forState:UIControlStateDisabled];
        return item;
    }
    
    - (instancetype)initWithImage:(UIImage *)normal
                         callback:(dispatch_block_t)callback {
        PRBarButtonItem *item = [super initWithImage:[normal imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)];
        item.callback = callback;
        return item;
    }
    
    - (void)onClick:(id)sender {
        if (self.callback) {
            self.callback();
        }
    }
    
    //带文字与图片 
    + (UIBarButtonItem *)getBarBtnItemWithTarget:(id)target withSEL:(SEL)sel  image:(NSString *)imageStr title:(NSString *)string{
        UIButton *someButton = [[UIButton alloc]init];
        [someButton setFrame:CGRectMake(0, 0, 22, 30)];
        [someButton setImage:[UIImage imageNamed:imageStr] forState:0];
        [someButton addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
        [someButton setTitleColor: UIColorRGB(0x333333) forState:0];
        someButton.titleLabel.font = [UIFont systemFontOfSize:10];
        [someButton setTitle:string forState:0];
        someButton.titleEdgeInsets = UIEdgeInsetsMake(20, -7,0, 0);
        someButton.imageEdgeInsets = UIEdgeInsetsMake(-10, 22, 0, 0);
        
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:someButton];
        return item;
    }
    
    使用下
       PRBarButtonItem *shareBarItem = [PRBarButtonItem getWithImage:[UIImage imageNamed:""] callback:^{
            
        }];
        PRBarButtonItem *mailBarItem = [PRBarButtonItem getWithTitle:@"邮件" callback:^{
            [weakSelf operator];
        }];
        self.navigationItem.rightBarButtonItems = @[ shareBarItem, mailBarItem ];
    

    相关文章

      网友评论

          本文标题:iOS 封装一下ButtonItem

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