// // UIButton-Extensions.swift // HPlusFit // // Created by lemo. on 2019/9/15. // Copyright © 2019 lemo. All rights reserved. // import Foundation extension UIButton { /// 修改按钮图片与文职的位置 /// /// - Parameters: /// - type: 0: 默认 图左字右边 1:图右字左 2:图上字下 3:字下图上 /// - spacing: 图片与文字的间距,左右或上线 func changgeImageAndTitleEdgeInsets(type: Int, spacing: CGFloat) { switch type { case 1: if let titleLabel = titleLabel, let image = imageView?.image { titleLabel.sizeToFit() let labelWidth = titleLabel.width + spacing let imageWidth = image.size.width imageEdgeInsets = UIEdgeInsets(top: 0, left: labelWidth, bottom: 0, right: -labelWidth) titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth, bottom: 0, right: imageWidth) } default: break } } /// 导航栏按钮(图片) static func barButtonItem(_ normalImageName: String, _ highImageName: String?, _ target: Any?, _ action: Selector) -> UIButton { let button = UIButton(type: .custom) let normalImage = UIImage(named: normalImageName) button.setImage(normalImage, for: .normal) button.frame.size = (normalImage?.size)! if highImageName != nil { button.setImage(UIImage(named: highImageName!), for: .highlighted) } button.addTarget(target, action: action, for: .touchUpInside) return button } /// 导航栏按钮(文字) static func barButtonItem(_ title: String?, _ target: Any?, _ action: Selector) -> UIButton { let button = UIButton(type: .custom) // button.titleLabel?.font = kNavBarItemFont // button.setTitleColor(kNavBarItemTextColor, for: .normal) button.setTitle(title, for: .normal) button.addTarget(target, action: action, for: .touchUpInside) button.sizeToFit() return button } }