MYCSDN:samuelandkevin
// OC版
// UIBarButtonItem+Custom.h
// samuelandkevin
//
// Created by samuelandkevin on 16/11/30.
// Copyright © 2016年 samuelandkevin. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIBarButtonItem (Custom)
/*
*默认的左返回按钮
*/
+ (UIBarButtonItem *)backItemWithTarget:(id)target selector:(SEL)selector;
/**********UIBarButtonItem为文字的设置**********/
+ (UIBarButtonItem *)leftItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector;
+ (UIBarButtonItem *)leftItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector block:(void(^)(UIButton *btn))block;
+ (UIBarButtonItem *)rightItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector;
+ (UIBarButtonItem *)rightItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector block:(void(^)(UIButton *btn))block;
/**********UIBarButtonItem为图片的设置**********/
/*
*通用leftBarItem imgName:图片名 target:目标对象 selector:相应方法
*/
+ (UIBarButtonItem *)leftItemWithImgName:(NSString *)imgName target:(id)target selector:(SEL)selector;
+ (UIBarButtonItem *)leftItemWithImgName:(NSString *)imgName target:(id)target selector:(SEL)selector block:(void(^)(UIButton *btn))block;
/*
*通用rightBarItem imgName:图片名 target:目标对象 selector:相应方法
*/
+ (UIBarButtonItem *)rightItemWithImgName:(NSString *)imgName target:(id)target selector:(SEL)selector;
+ (UIBarButtonItem *)rightItemWithImgName:(NSString *)imgName target:(id)target selector:(SEL)selector block:(void(^)(UIButton *btn))block;
@end
//
// UIBarButtonItem+Custom.m
// samuelandkevin
//
// Created by samuelandkevin on 16/11/30.
// Copyright © 2016年 samuelandkevin. All rights reserved.
//
#import "UIBarButtonItem+Custom.h"
#import <objc/runtime.h>
static const char *btnBlockKey = "btnBlockKey";
@interface UIBarButtonItem()
@property (nonatomic,copy) void(^btnBlock)();
@end
@implementation UIBarButtonItem (Custom)
- (void (^)())btnBlock{
return objc_getAssociatedObject(self, btnBlockKey);
}
- (void)setBtnBlock:(void (^)())btnBlock{
objc_setAssociatedObject(self, btnBlockKey, btnBlock, OBJC_ASSOCIATION_COPY);
}
#pragma mark - Public
+ (UIBarButtonItem *)backItemWithTarget:(id)target selector:(SEL)selector{
return [self barButtonItemWithFrame:CGRectMake(0, 0, 40, 40) imgName:@"leftarrow" imageEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0) target:target selector:selector];
}
/**********UIBarButtonItem为文字的设置**********/
+ (UIBarButtonItem *)leftItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector{
return [self barButtonItemWithFrame:CGRectMake(0, 0, 40, 40) title:title target:target selector:selector block:nil];
}
+ (UIBarButtonItem *)leftItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector block:(void(^)(UIButton *btn))block{
return [self barButtonItemWithFrame:CGRectMake(0, 0, 40, 40) title:title target:target selector:selector block:block];
}
+ (UIBarButtonItem *)rightItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector{
return [self barButtonItemWithFrame:CGRectMake(0, 0, 40, 40) title:title target:target selector:selector block:nil];
}
+ (UIBarButtonItem *)rightItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector block:(void(^)(UIButton *btn))block{
return [self barButtonItemWithFrame:CGRectMake(0, 0, 40, 40) title:title target:target selector:selector block:block];
}
/**********UIBarButtonItem为图片的设置**********/
+ (UIBarButtonItem *)leftItemWithImgName:(NSString *)imgName target:(id)target selector:(SEL)selector{
return [self barButtonItemWithFrame:CGRectMake(0, 0, 44, 44) imgName:imgName imageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20) target:target selector:selector];
}
+ (UIBarButtonItem *)leftItemWithImgName:(NSString *)imgName target:(id)target selector:(SEL)selector block:(void(^)(UIButton *btn))block{
return [self barButtonItemWithFrame:CGRectMake(0, 0, 44, 44) imgName:imgName imageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20) target:target selector:selector block:block];
}
+ (UIBarButtonItem *)rightItemWithImgName:(NSString *)imgName target:(id)target selector:(SEL)selector{
return [self barButtonItemWithFrame:CGRectMake(0, 0, 44, 44) imgName:imgName imageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -20) target:target selector:selector];
}
+ (UIBarButtonItem *)rightItemWithImgName:(NSString *)imgName target:(id)target selector:(SEL)selector block:(void(^)(UIButton *btn))block{
return [self barButtonItemWithFrame:CGRectMake(0, 0, 44, 44) imgName:imgName imageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -20) target:target selector:selector block:block];
}
#pragma mark - Private
+ (UIButton *)barButtonWithFrame:(CGRect)frame imgName:(NSString *)imgName imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets target:(id)target selector:(SEL)selector{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = frame;
[btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:18];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
btn.imageEdgeInsets = imageEdgeInsets;
return btn;
}
+ (UIBarButtonItem *)barButtonItemWithFrame:(CGRect)frame imgName:(NSString *)imgName imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets target:(id)target selector:(SEL)selector {
UIButton *btn = [self barButtonWithFrame:frame imgName:imgName imageEdgeInsets:imageEdgeInsets target:target selector:selector];
return [[UIBarButtonItem alloc] initWithCustomView:btn];
}
+ (UIBarButtonItem *)barButtonItemWithFrame:(CGRect)frame imgName:(NSString *)imgName imageEdgeInsets:(UIEdgeInsets)imageEdgeInsets target:(id)target selector:(SEL)selector block:(void(^)(UIButton *btn))block{
UIButton *btn = [self barButtonWithFrame:frame imgName:imgName imageEdgeInsets:imageEdgeInsets target:target selector:selector];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
if (block) {
item.btnBlock = block;
if (item.btnBlock) {
item.btnBlock(btn);
}
}
return item;
}
+ (UIBarButtonItem *)barButtonItemWithFrame:(CGRect)frame title:(NSString *)title target:(id)target selector:(SEL)selector block:(void(^)(UIButton *btn))block{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = frame;
[btn setTitle:title forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:18];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
if (block) {
item.btnBlock = block;
if (item.btnBlock) {
item.btnBlock(btn);
}
}
return item;
}
@end
// Swift版
// UIBarButtonItem+Extension.swift
// samuelandkevin
//
// Created by samuelandkevin on 2017/5/8.
// Copyright © 2017年 samuelandkevin. All rights reserved.
//
import Foundation
let btnBlockKey = "btnBlockKey"
typealias BtnBlock = ((_ btn:UIButton) -> Void)?
extension UIBarButtonItem {
//定义按钮闭包
var btnBlock: BtnBlock {
get{
return objc_getAssociatedObject(self, btnBlockKey) as? (UIButton)->Void
}
set{
objc_setAssociatedObject(self, btnBlockKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
}
}
// MARK: - Public Method
/// 默认的左返回按钮
///
/// - Parameters:
/// - target: 目标
/// - selector: 方法
/// - Returns: UIBarButtonItem
internal class func backItem(target:Any,selector:Selector) -> UIBarButtonItem{
return _barButtonItem(frame: CGRect(x: 0, y: 0, width: 40, height: 40), imgName: "leftarrow", imageEdgeInsets: UIEdgeInsetsMake(0, -30, 0, 0), target: target, selector: selector)
}
/**********UIBarButtonItem为文字的设置**********/
/// 左Item为纯文字
///
/// - Parameters:
/// - title: 标题
/// - target: 目标
/// - selector: 方法
/// - Returns: UIBarButtonItem
internal class func leftItem(title:String,target:Any,selector:Selector) -> UIBarButtonItem{
return _barButtonItem(frame: CGRect(x: 0, y: 0, width: 40, height: 40), title: title, target: target, selector: selector, block: nil)
}
/// 左Item为纯文字
///
/// - Parameters:
/// - title: 标题
/// - target: 目标
/// - selector: 方法
/// - block: 按钮回调
/// - Returns: UIBarButtonItem
internal class func leftItem(title:String,target:Any,selector:Selector,block:BtnBlock) -> UIBarButtonItem{
return _barButtonItem(frame: CGRect(x: 0, y: 0, width: 40, height: 40), title: title, target: target, selector: selector, block: block)
}
/// 右Item为纯文字
///
/// - Parameters:
/// - title: 标题
/// - target: 目标
/// - selector: 方法
/// - Returns: UIBarButtonItem
internal class func rightItem(title:String,target:Any,selector:Selector) -> UIBarButtonItem{
return _barButtonItem(frame: CGRect(x: 0, y: 0, width: 40, height: 40), title: title, target: target, selector: selector, block: nil)
}
/// 右Item为纯文字
///
/// - Parameters:
/// - title: 标题
/// - target: 目标
/// - selector: 方法
/// - block: 按钮回调
/// - Returns: UIBarButtonItem
internal class func rightItem(title:String,target:Any,selector:Selector,block:BtnBlock) -> UIBarButtonItem{
return _barButtonItem(frame: CGRect(x: 0, y: 0, width: 40, height: 40), title: title, target: target, selector: selector, block: block)
}
/**********UIBarButtonItem为图片的设置**********/
/// 左Item为纯图片
///
/// - Parameters:
/// - imgName: 图片名
/// - target: 目标
/// - selector: 方法
/// - Returns: UIBarButtonItem
internal class func leftItem(imgName:String,target:Any,selector:Selector) -> UIBarButtonItem{
return _barButtonItem(frame: CGRect(x: 0, y: 0, width: 44, height: 44), imgName: imgName, imageEdgeInsets: UIEdgeInsetsMake(0, 0, 0, 20), target: target, selector: selector)
}
/// 左Item为纯图片
///
/// - Parameters:
/// - imgName: 图片名
/// - target: 目标
/// - selector: 方法
/// - block: 按钮回调
/// - Returns: UIBarButtonItem
internal class func leftItem(imgName:String,target:Any,selector:Selector,block:BtnBlock) -> UIBarButtonItem{
return _barButtonItem(frame: CGRect(x: 0, y: 0, width: 44, height: 44), imgName: imgName, imageEdgeInsets: UIEdgeInsetsMake(0, 0, 0, 20), target: target, selector: selector,block:block)
}
/// 右Item为纯图片
///
/// - Parameters:
/// - imgName: 图片名
/// - target: 目标
/// - selector: 方法
/// - Returns: UIBarButtonItem
internal class func rightItem(imgName:String,target:Any,selector:Selector) -> UIBarButtonItem{
return _barButtonItem(frame: CGRect(x: 0, y: 0, width: 44, height: 44), imgName: imgName, imageEdgeInsets: UIEdgeInsetsMake(0, 0, 0, -20), target: target, selector: selector)
}
/// 右Item为纯图片
///
/// - Parameters:
/// - imgName: 图片名
/// - target: 目标
/// - selector: 方法
/// - block: 按钮回调
/// - Returns: UIBarButtonItem
internal class func rightItem(imgName:String,target:Any,selector:Selector,block:BtnBlock) -> UIBarButtonItem{
return _barButtonItem(frame: CGRect(x: 0, y: 0, width: 44, height: 44), imgName: imgName, imageEdgeInsets: UIEdgeInsetsMake(0, 0, 0, -20), target: target, selector: selector,block:block)
}
// MARK: - Private Method
internal class func _barButton(frame:CGRect,imgName:String,imageEdgeInsets:UIEdgeInsets,target:Any,selector:Selector) -> UIButton{
let btn = UIButton(type: .system)
btn.frame = frame
btn.setImage(UIImage(named:imgName), for: .normal)
btn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
btn.titleLabel?.textColor = UIColor.white
btn.addTarget(target, action: selector, for: .touchUpInside)
btn.imageEdgeInsets = imageEdgeInsets
return btn
}
internal class func _barButtonItem(frame:CGRect,imgName:String,imageEdgeInsets:UIEdgeInsets,target:Any,selector:Selector) -> UIBarButtonItem {
let btn = _barButton(frame: frame, imgName: imgName, imageEdgeInsets: imageEdgeInsets, target: target, selector: selector)
let barButtonItem = UIBarButtonItem(customView: btn)
return barButtonItem
}
internal class func _barButtonItem(frame:CGRect,imgName:String,imageEdgeInsets:UIEdgeInsets,target:Any,selector:Selector,block:BtnBlock )-> UIBarButtonItem{
let aBtn = _barButton(frame: frame, imgName: imgName, imageEdgeInsets: imageEdgeInsets, target: target, selector: selector)
let item = UIBarButtonItem(customView: aBtn)
if ((block) != nil) {
item.btnBlock = block
if ((item.btnBlock) != nil) {
item.btnBlock!(aBtn)
}
}
return item;
}
internal class func _barButtonItem(frame:CGRect,title:String,target:Any,selector:Selector,block:BtnBlock ) -> UIBarButtonItem{
let btn = UIButton(type: .system)
btn.frame = frame
btn.setTitle(title, for: .normal)
btn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
btn.titleLabel?.textColor = UIColor.white
btn.addTarget(target, action: selector, for: .touchUpInside)
let item = UIBarButtonItem(customView: btn)
if ((block) != nil) {
item.btnBlock = block
if ((item.btnBlock) != nil) {
item.btnBlock!(btn)
}
}
return item
}
}