当前位置:网站首页>Extend uibarbuttonitem

Extend uibarbuttonitem

2020-12-07 08:32:04 Sword dragon Yuwu

//
//  WYBarButtonItem+Extension.swift
//  DYWX
//
//  Created by  Wang Wu  on 2020/11/24.
//

import UIKit

extension UIBarButtonItem {
    //  Class method creation UIBarButtonItem
    class func createBarButton(_ normalImageName: String, _ highlightImageName: String, _ size: CGSize) -> UIBarButtonItem {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: normalImageName), for: .normal)
        btn.setImage(UIImage(named: highlightImageName), for: .highlighted)
        btn.frame = CGRect(origin: CGPoint.zero, size: size)
        
        return UIBarButtonItem(customView: btn)
    }
    
    //  Instance method creation , Convenience constructors : 1. You have to use  convenience 2. A designed constructor must be used in the constructor self
    convenience init(normalImageName: String, highlightImageName: String = "", size: CGSize = CGSize.zero) {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: normalImageName), for: .normal)
        
        if highlightImageName != "" {
            btn.setImage(UIImage(named: highlightImageName), for: .highlighted)
        }
        
        if size != CGSize.zero {
            btn.frame = CGRect(origin: CGPoint.zero, size: size)
        } else {
            btn.sizeToFit()
        }
        
        self.init(customView: btn)
    }
}

版权声明
本文为[Sword dragon Yuwu]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012070830062001.html