123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- //
- // WRCustomNavigationBar.swift
- // WRNavigationBar_swift
- //
- // Created by itwangrui on 2017/11/25.
- // Copyright © 2017年 wangrui. All rights reserved.
- //
- import UIKit
- import DeviceKit
- fileprivate let WRDefaultTitleSize:CGFloat = 18
- fileprivate let WRDefaultTitleColor = UIColor.black
- fileprivate let WRDefaultBackgroundColor = UIColor.white
- fileprivate let WRScreenWidth = UIScreen.main.bounds.size.width
- // MARK: - Router
- extension UIViewController
- {
- // A页面 弹出 登录页面B
- // presentedViewController: A页面
- // presentingViewController: B页面
-
- func wr_toLastViewController(animated:Bool)
- {
- if self.navigationController != nil
- {
- if self.navigationController?.viewControllers.count == 1
- {
- self.dismiss(animated: animated, completion: nil)
- } else {
- self.navigationController?.popViewController(animated: animated)
- }
- }
- else if self.presentingViewController != nil {
- self.dismiss(animated: animated, completion: nil)
- }
- }
-
- class func wr_currentViewController() -> UIViewController
- {
- if let rootVC = UIApplication.shared.delegate?.window??.rootViewController {
- return self.wr_currentViewController(from: rootVC)
- } else {
- return UIViewController()
- }
- }
- class func wr_currentViewController(from fromVC:UIViewController) -> UIViewController
- {
- if fromVC.isKind(of: UINavigationController.self) {
- let navigationController = fromVC as! UINavigationController
- return wr_currentViewController(from: navigationController.viewControllers.last!)
- }
- else if fromVC.isKind(of: UITabBarController.self) {
- let tabBarController = fromVC as! UITabBarController
- return wr_currentViewController(from: tabBarController.selectedViewController!)
- }
- else if fromVC.presentedViewController != nil {
- return wr_currentViewController(from:fromVC.presentingViewController!)
- }
- else {
- return fromVC
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////
- class WRCustomNavigationBar: UIView
- {
- var onClickLeftButton:(()->())?
- var onClickRightButton:(()->())?
- var title:String? {
- willSet {
- titleLabel.isHidden = false
- titleLabel.text = newValue
- }
- }
- var titleLabelColor:UIColor? {
- willSet {
- titleLabel.textColor = newValue
- }
- }
- var titleLabelFont:UIFont? {
- willSet {
- titleLabel.font = newValue
- }
- }
- var barBackgroundColor:UIColor? {
- willSet {
- backgroundImageView.isHidden = true
- backgroundView.isHidden = false
- backgroundView.backgroundColor = newValue
- }
- }
- var barBackgroundImage:UIImage? {
- willSet {
- backgroundView.isHidden = true
- backgroundImageView.isHidden = false
- backgroundImageView.image = newValue
- }
- }
-
- // fileprivate UI variable
- lazy var titleLabel:UILabel = {
- let label = UILabel()
- label.textColor = WRDefaultTitleColor
- label.font = UIFont.systemFont(ofSize: WRDefaultTitleSize)
- label.textAlignment = .center
- label.isHidden = true
- return label
- }()
-
- lazy var leftButton:UIButton = {
- let button = UIButton()
- button.imageView?.contentMode = .center
- button.isHidden = true
- button.addTarget(self, action: #selector(clickBack), for: .touchUpInside)
- button.titleLabel?.font = UIFont(name: "PingFang-SC-Regular", size: 14)
- return button
- }()
-
- lazy var rightButton:UIButton = {
- let button = UIButton()
- button.imageView?.contentMode = .center
- button.isHidden = true
- button.addTarget(self, action: #selector(clickRight), for: .touchUpInside)
- button.titleLabel?.font = UIFont(name: "PingFang-SC-Regular", size: 14)
- return button
- }()
-
- fileprivate lazy var bottomLine:UIImageView = {
- let bottomLine = UIImageView()
- bottomLine.image = kImage(name: "navbar_shadow_pic_up")
- return bottomLine
- }()
-
- fileprivate lazy var backgroundView:UIView = {
- let view = UIView()
- return view
- }()
-
- lazy var backgroundImageView:UIImageView = {
- let imgView = UIImageView()
- imgView.isHidden = true
- return imgView
- }()
-
- ///是不是.iPhoneX,.iPhoneXr,.iPhoneXs,.iPhoneXsMax
- fileprivate static var isIphoneX:Bool {
- get {
- let groupOfAllowedDevices: [Device] = [.iPhoneX,.iPhoneXR,.iPhoneXS,.iPhoneXSMax,.simulator(.iPhoneX),.simulator(.iPhoneXR),.simulator(.iPhoneXS),.simulator(.iPhoneXSMax)]
- let device = Device.current
- if device.isOneOf(groupOfAllowedDevices) {
- return true
- }else {
- return false
- }
- }
- }
- fileprivate static var navBarBottom:Int {
- get {
- return isIphoneX ? 88 : 64
- }
- }
-
- // init
- class func CustomNavigationBar() -> WRCustomNavigationBar {
- let frame = CGRect(x: 0, y: 0, width: WRScreenWidth, height: CGFloat(navBarBottom))
- return WRCustomNavigationBar(frame: frame)
- }
- override init(frame: CGRect) {
- super.init(frame: frame)
- setupView()
- }
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- setupView()
- }
-
- func setupView()
- {
- addSubview(backgroundView)
- addSubview(backgroundImageView)
- addSubview(leftButton)
- addSubview(titleLabel)
- addSubview(rightButton)
- addSubview(bottomLine)
- updateFrame()
- backgroundColor = UIColor.clear
- backgroundView.backgroundColor = WRDefaultBackgroundColor
- }
- func updateFrame()
- {
- let top:CGFloat = WRCustomNavigationBar.isIphoneX ? 44 : 20
- let margin:CGFloat = 0
- let buttonHeight:CGFloat = 44
- let buttonWidth:CGFloat = 44
- let titleLabelHeight:CGFloat = 44
- let titleLabelWidth:CGFloat = 180
-
- backgroundView.frame = self.bounds
- backgroundImageView.frame = self.bounds
- leftButton.frame = CGRect(x: margin, y: top, width: buttonWidth, height: buttonHeight)
- rightButton.frame = CGRect(x: WRScreenWidth-buttonWidth-margin, y: top, width: buttonWidth, height: buttonHeight)
- titleLabel.frame = CGRect(x: (WRScreenWidth-titleLabelWidth)/2.0, y: top, width: titleLabelWidth, height: titleLabelHeight)
- bottomLine.frame = CGRect(x: 0, y: bounds.height-0.5, width: WRScreenWidth, height: 20)
- }
- }
- extension WRCustomNavigationBar
- {
- func wr_setBottomLineHidden(hidden:Bool) {
- bottomLine.isHidden = hidden
- }
- func wr_setBackgroundAlpha(alpha:CGFloat) {
- backgroundView.alpha = alpha
- backgroundImageView.alpha = alpha
- bottomLine.alpha = alpha
- }
- func wr_setTintColor(color:UIColor) {
- leftButton.setTitleColor(color, for: .normal)
- rightButton.setTitleColor(color, for: .normal)
- titleLabel.textColor = color
- }
-
- // 左右按钮共有方法
- func wr_setLeftButton(normal:UIImage, highlighted:UIImage) {
- wr_setLeftButton(normal: normal, highlighted: highlighted, title: nil, titleColor: nil)
- leftButton.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kSafeStatusBarHeight)
- make.left.equalToSuperview()
- make.size.equalTo(44)
- }
- }
- func wr_setLeftButton(image:UIImage) {
- wr_setLeftButton(normal: image, highlighted: image, title: nil, titleColor: nil)
- leftButton.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kSafeStatusBarHeight)
- make.left.equalToSuperview()
- make.size.equalTo(44)
- }
- }
- func wr_setLeftButton(title:String, titleColor:UIColor) {
- wr_setLeftButton(normal: nil, highlighted: nil, title: title, titleColor: titleColor)
- leftButton.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kSafeStatusBarHeight)
- make.left.equalToSuperview()
- make.height.equalTo(44)
- }
- }
-
- func wr_setRightButton(normal:UIImage, highlighted:UIImage) {
- wr_setRightButton(normal: normal, highlighted: highlighted, title: nil, titleColor: nil)
- rightButton.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kSafeStatusBarHeight)
- make.right.equalToSuperview()
- make.size.equalTo(44)
- }
- }
- func wr_setRightButton(image:UIImage) {
- wr_setRightButton(normal: image, highlighted: image, title: nil, titleColor: nil)
- rightButton.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kSafeStatusBarHeight)
- make.right.equalToSuperview()
- make.size.equalTo(44)
- }
- }
- func wr_setRightButton(title:String, titleColor:UIColor) {
- wr_setRightButton(normal: nil, highlighted: nil, title: title, titleColor: titleColor)
- rightButton.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(kSafeStatusBarHeight)
- make.right.equalToSuperview().offset(-16)
- make.height.equalTo(44)
- }
- }
-
-
- // 左右按钮私有方法
- private func wr_setLeftButton(normal:UIImage?, highlighted:UIImage?, title:String?, titleColor:UIColor?) {
- leftButton.isHidden = false
- leftButton.setImage(normal, for: .normal)
- leftButton.setImage(highlighted, for: .highlighted)
- leftButton.setTitle(title, for: .normal)
- leftButton.setTitleColor(titleColor, for: .normal)
- }
- private func wr_setRightButton(normal:UIImage?, highlighted:UIImage?, title:String?, titleColor:UIColor?) {
- rightButton.isHidden = false
- rightButton.setImage(normal, for: .normal)
- rightButton.setImage(highlighted, for: .highlighted)
- rightButton.setTitle(title, for: .normal)
- rightButton.setTitleColor(titleColor, for: .normal)
- }
- }
- // MARK: - 导航栏左右按钮事件
- extension WRCustomNavigationBar
- {
- @objc func clickBack() {
- if let onClickBack = onClickLeftButton {
- onClickBack()
- }
- }
- @objc func clickRight() {
- if let onClickRight = onClickRightButton {
- onClickRight()
- }
- }
- }
|