最近在学习swift的过程中,看到了一些自定义UIBarButtonItem的例子,再此想和大家分享一下,废话不多说,先看下效果图:
1 重写Button的titleLabel和imageView两个属性
class ItemLeftButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
let Offset: CGFloat = 15
titleLabel?.sizeToFit()
titleLabel?.frame = CGRectMake(-Offset, height - 15, width - Offset, (titleLabel?.height)!)
titleLabel?.textAlignment = .Center
imageView?.frame = CGRectMake(-Offset, 0, width - Offset, height - 15)
imageView?.contentMode = .Center
}
}
class ItemRightButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
let Offset: CGFloat = 15
titleLabel?.sizeToFit()
titleLabel?.frame = CGRectMake(Offset, height - 15, width + Offset, (titleLabel?.height)!)
titleLabel?.textAlignment = .Center
imageView?.frame = CGRectMake(Offset, 0, width + Offset, height - 15)
imageView?.contentMode = UIViewContentMode.Center
}
}
2 自定义UIBarButtonItem的方法
enum ItemButtonType: Int {
case Left = 0
case Right = 1
}
extension UIBarButtonItem {
class func barButton(title: String, titleColor: UIColor, image: UIImage, hightLightImage: UIImage?, target: AnyObject?, action: Selector, type: ItemButtonType) -> UIBarButtonItem {
var btn:UIButton = UIButton()
if type == ItemButtonType.Left {
btn = ItemLeftButton(type: .Custom)
} else {
btn = ItemRightButton(type: .Custom)
}
btn.setTitle(title, forState: .Normal)
btn.setImage(image, forState: .Normal)
btn.setTitleColor(titleColor, forState: .Normal)
btn.setImage(hightLightImage, forState: .Highlighted)
btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
btn.frame = CGRectMake(0, 0, 60, 44)
btn.titleLabel?.font = UIFont.systemFontOfSize(10)
return UIBarButtonItem(customView: btn)
}
class func barButton(image: UIImage, target: AnyObject?, action: Selector) -> UIBarButtonItem {
let btn = ItemLeftButton(type: .Custom)
btn.setImage(image, forState: UIControlState.Normal)
btn.imageView?.contentMode = UIViewContentMode.Center
btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
btn.frame = CGRectMake(0, 0, 44, 44)
return UIBarButtonItem(customView: btn)
}
class func barButton(title: String, titleColor: UIColor, target: AnyObject?, action: Selector) -> UIBarButtonItem {
let btn = UIButton(frame: CGRectMake(0, 0, 60, 44))
btn.setTitle(title, forState: .Normal)
btn.setTitleColor(titleColor, forState: .Normal)
btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
btn.titleLabel?.font = UIFont.systemFontOfSize(15)
if title.characters.count == 2 {
btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -25)
}
return UIBarButtonItem(customView: btn)
}
}
3 对UIView进行扩展
extension UIView {
/// X值
var x: CGFloat {
return self.frame.origin.x
}
/// Y值
var y: CGFloat {
return self.frame.origin.y
}
/// 宽度
var width: CGFloat {
return self.frame.size.width
}
///高度
var height: CGFloat {
return self.frame.size.height
}
var size: CGSize {
return self.frame.size
}
var point: CGPoint {
return self.frame.origin
}
}
4 调用方法
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem.barButton("扫一扫", titleColor: UIColor.blackColor(), image: UIImage(named: "icon_black_scancode")!, hightLightImage: nil, target: self, action: #selector(ViewController.leftItemClick), type: ItemButtonType.Left)
}
func leftItemClick() {
print("扫一扫")
}
}