UIBarButtonItem添加一个分类

gistfile1.txt
extension UIBarButtonItem {
    
    class func creatBarButtonItem(_ ImageName:String,target:Any?,action:Selector) -> UIBarButtonItem {
        
        let Btn = UIButton()
        Btn.setImage(UIImage(named: ImageName), for: UIControlState.normal)
        Btn.setImage(UIImage(named: ImageName + "_highlighted"), for: UIControlState.highlighted)
        Btn.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        Btn.sizeToFit()
        return UIBarButtonItem(customView: Btn)
        
    }
}
UIBarButtonItem Extension.swift
//
//  UIBarButtonItem+Extension.swift
//  tableView
//
//  Created by LYW on 2018/3/20.
//  Copyright © 2018年 XiaozhiTechnology. All rights reserved.
//

import UIKit

extension UIBarButtonItem
{
    class func creatBarButtonItemImage(target:Any?,action:Selector,ImageName:String) -> UIBarButtonItem {
        let Btn = UIButton()
        Btn.setImage(UIImage(named: ImageName), for: .normal)
        Btn.setImage(UIImage(named: ImageName + "-click"), for: .highlighted)
        Btn.addTarget(target, action: action, for: .touchUpInside)
        
        return UIBarButtonItem(customView: Btn)
    }
    
    
   class func creatBarButtonItemTitle(target:Any?,action:Selector,title:String) -> UIBarButtonItem {
        let Btn = UIButton()
        Btn.setTitle(title, for: .normal)
        Btn.setTitleColor(UIColor.black, for: .normal)
        Btn.setTitleColor(UIColor.red, for: .highlighted)
        Btn.addTarget(target, action:action, for: .touchUpInside)
        
        return UIBarButtonItem(customView:Btn)
    }
    
    class func creatBarButtonItemTitleAndImage(target:Any?,action:Selector,ImageName:String,title:String) -> UIBarButtonItem {
        let Btn = UIButton()
        Btn.setTitle(title, for: .normal)
        Btn.setImage(UIImage(named: ImageName), for: .normal)
        Btn.setImage(UIImage(named: ImageName + "-click"), for: .highlighted)
        Btn.setTitleColor(UIColor.black, for: .normal)
        Btn.setTitleColor(UIColor.red, for: .highlighted)
        Btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
        Btn.titleEdgeInsets = UIEdgeInsetsMake(3, 0, 0, 0)
        Btn.addTarget(target, action:action, for: .touchUpInside)
        Btn.sizeToFit()
        
        return UIBarButtonItem(customView:Btn)
    }
    
}
相关代码

相关文章

相关标签/搜索