WRCustomNavigationBar.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // WRCustomNavigationBar.swift
  3. // WRNavigationBar_swift
  4. //
  5. // Created by itwangrui on 2017/11/25.
  6. // Copyright © 2017年 wangrui. All rights reserved.
  7. //
  8. import UIKit
  9. import DeviceKit
  10. fileprivate let WRDefaultTitleSize:CGFloat = 18
  11. fileprivate let WRDefaultTitleColor = UIColor.black
  12. fileprivate let WRDefaultBackgroundColor = UIColor.white
  13. fileprivate let WRScreenWidth = UIScreen.main.bounds.size.width
  14. // MARK: - Router
  15. extension UIViewController
  16. {
  17. // A页面 弹出 登录页面B
  18. // presentedViewController: A页面
  19. // presentingViewController: B页面
  20. func wr_toLastViewController(animated:Bool)
  21. {
  22. if self.navigationController != nil
  23. {
  24. if self.navigationController?.viewControllers.count == 1
  25. {
  26. self.dismiss(animated: animated, completion: nil)
  27. } else {
  28. self.navigationController?.popViewController(animated: animated)
  29. }
  30. }
  31. else if self.presentingViewController != nil {
  32. self.dismiss(animated: animated, completion: nil)
  33. }
  34. }
  35. class func wr_currentViewController() -> UIViewController
  36. {
  37. if let rootVC = UIApplication.shared.delegate?.window??.rootViewController {
  38. return self.wr_currentViewController(from: rootVC)
  39. } else {
  40. return UIViewController()
  41. }
  42. }
  43. class func wr_currentViewController(from fromVC:UIViewController) -> UIViewController
  44. {
  45. if fromVC.isKind(of: UINavigationController.self) {
  46. let navigationController = fromVC as! UINavigationController
  47. return wr_currentViewController(from: navigationController.viewControllers.last!)
  48. }
  49. else if fromVC.isKind(of: UITabBarController.self) {
  50. let tabBarController = fromVC as! UITabBarController
  51. return wr_currentViewController(from: tabBarController.selectedViewController!)
  52. }
  53. else if fromVC.presentedViewController != nil {
  54. return wr_currentViewController(from:fromVC.presentingViewController!)
  55. }
  56. else {
  57. return fromVC
  58. }
  59. }
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62. class WRCustomNavigationBar: UIView
  63. {
  64. var onClickLeftButton:(()->())?
  65. var onClickRightButton:(()->())?
  66. var title:String? {
  67. willSet {
  68. titleLabel.isHidden = false
  69. titleLabel.text = newValue
  70. }
  71. }
  72. var titleLabelColor:UIColor? {
  73. willSet {
  74. titleLabel.textColor = newValue
  75. }
  76. }
  77. var titleLabelFont:UIFont? {
  78. willSet {
  79. titleLabel.font = newValue
  80. }
  81. }
  82. var barBackgroundColor:UIColor? {
  83. willSet {
  84. backgroundImageView.isHidden = true
  85. backgroundView.isHidden = false
  86. backgroundView.backgroundColor = newValue
  87. }
  88. }
  89. var barBackgroundImage:UIImage? {
  90. willSet {
  91. backgroundView.isHidden = true
  92. backgroundImageView.isHidden = false
  93. backgroundImageView.image = newValue
  94. }
  95. }
  96. // fileprivate UI variable
  97. lazy var titleLabel:UILabel = {
  98. let label = UILabel()
  99. label.textColor = WRDefaultTitleColor
  100. label.font = UIFont.systemFont(ofSize: WRDefaultTitleSize)
  101. label.textAlignment = .center
  102. label.isHidden = true
  103. return label
  104. }()
  105. lazy var leftButton:UIButton = {
  106. let button = UIButton()
  107. button.imageView?.contentMode = .center
  108. button.isHidden = true
  109. button.addTarget(self, action: #selector(clickBack), for: .touchUpInside)
  110. button.titleLabel?.font = UIFont(name: "PingFang-SC-Regular", size: 14)
  111. return button
  112. }()
  113. lazy var rightButton:UIButton = {
  114. let button = UIButton()
  115. button.imageView?.contentMode = .center
  116. button.isHidden = true
  117. button.addTarget(self, action: #selector(clickRight), for: .touchUpInside)
  118. button.titleLabel?.font = UIFont(name: "PingFang-SC-Regular", size: 14)
  119. return button
  120. }()
  121. fileprivate lazy var bottomLine:UIImageView = {
  122. let bottomLine = UIImageView()
  123. bottomLine.image = kImage(name: "navbar_shadow_pic_up")
  124. return bottomLine
  125. }()
  126. fileprivate lazy var backgroundView:UIView = {
  127. let view = UIView()
  128. return view
  129. }()
  130. lazy var backgroundImageView:UIImageView = {
  131. let imgView = UIImageView()
  132. imgView.isHidden = true
  133. return imgView
  134. }()
  135. ///是不是.iPhoneX,.iPhoneXr,.iPhoneXs,.iPhoneXsMax
  136. fileprivate static var isIphoneX:Bool {
  137. get {
  138. let groupOfAllowedDevices: [Device] = [.iPhoneX,.iPhoneXR,.iPhoneXS,.iPhoneXSMax,.simulator(.iPhoneX),.simulator(.iPhoneXR),.simulator(.iPhoneXS),.simulator(.iPhoneXSMax)]
  139. let device = Device.current
  140. if device.isOneOf(groupOfAllowedDevices) {
  141. return true
  142. }else {
  143. return false
  144. }
  145. }
  146. }
  147. fileprivate static var navBarBottom:Int {
  148. get {
  149. return isIphoneX ? 88 : 64
  150. }
  151. }
  152. // init
  153. class func CustomNavigationBar() -> WRCustomNavigationBar {
  154. let frame = CGRect(x: 0, y: 0, width: WRScreenWidth, height: CGFloat(navBarBottom))
  155. return WRCustomNavigationBar(frame: frame)
  156. }
  157. override init(frame: CGRect) {
  158. super.init(frame: frame)
  159. setupView()
  160. }
  161. required init?(coder aDecoder: NSCoder) {
  162. super.init(coder: aDecoder)
  163. setupView()
  164. }
  165. func setupView()
  166. {
  167. addSubview(backgroundView)
  168. addSubview(backgroundImageView)
  169. addSubview(leftButton)
  170. addSubview(titleLabel)
  171. addSubview(rightButton)
  172. addSubview(bottomLine)
  173. updateFrame()
  174. backgroundColor = UIColor.clear
  175. backgroundView.backgroundColor = WRDefaultBackgroundColor
  176. }
  177. func updateFrame()
  178. {
  179. let top:CGFloat = WRCustomNavigationBar.isIphoneX ? 44 : 20
  180. let margin:CGFloat = 0
  181. let buttonHeight:CGFloat = 44
  182. let buttonWidth:CGFloat = 44
  183. let titleLabelHeight:CGFloat = 44
  184. let titleLabelWidth:CGFloat = 180
  185. backgroundView.frame = self.bounds
  186. backgroundImageView.frame = self.bounds
  187. leftButton.frame = CGRect(x: margin, y: top, width: buttonWidth, height: buttonHeight)
  188. rightButton.frame = CGRect(x: WRScreenWidth-buttonWidth-margin, y: top, width: buttonWidth, height: buttonHeight)
  189. titleLabel.frame = CGRect(x: (WRScreenWidth-titleLabelWidth)/2.0, y: top, width: titleLabelWidth, height: titleLabelHeight)
  190. bottomLine.frame = CGRect(x: 0, y: bounds.height-0.5, width: WRScreenWidth, height: 20)
  191. }
  192. }
  193. extension WRCustomNavigationBar
  194. {
  195. func wr_setBottomLineHidden(hidden:Bool) {
  196. bottomLine.isHidden = hidden
  197. }
  198. func wr_setBackgroundAlpha(alpha:CGFloat) {
  199. backgroundView.alpha = alpha
  200. backgroundImageView.alpha = alpha
  201. bottomLine.alpha = alpha
  202. }
  203. func wr_setTintColor(color:UIColor) {
  204. leftButton.setTitleColor(color, for: .normal)
  205. rightButton.setTitleColor(color, for: .normal)
  206. titleLabel.textColor = color
  207. }
  208. // 左右按钮共有方法
  209. func wr_setLeftButton(normal:UIImage, highlighted:UIImage) {
  210. wr_setLeftButton(normal: normal, highlighted: highlighted, title: nil, titleColor: nil)
  211. leftButton.snp.makeConstraints { (make) in
  212. make.top.equalToSuperview().offset(kSafeStatusBarHeight)
  213. make.left.equalToSuperview()
  214. make.size.equalTo(44)
  215. }
  216. }
  217. func wr_setLeftButton(image:UIImage) {
  218. wr_setLeftButton(normal: image, highlighted: image, title: nil, titleColor: nil)
  219. leftButton.snp.makeConstraints { (make) in
  220. make.top.equalToSuperview().offset(kSafeStatusBarHeight)
  221. make.left.equalToSuperview()
  222. make.size.equalTo(44)
  223. }
  224. }
  225. func wr_setLeftButton(title:String, titleColor:UIColor) {
  226. wr_setLeftButton(normal: nil, highlighted: nil, title: title, titleColor: titleColor)
  227. leftButton.snp.makeConstraints { (make) in
  228. make.top.equalToSuperview().offset(kSafeStatusBarHeight)
  229. make.left.equalToSuperview()
  230. make.height.equalTo(44)
  231. }
  232. }
  233. func wr_setRightButton(normal:UIImage, highlighted:UIImage) {
  234. wr_setRightButton(normal: normal, highlighted: highlighted, title: nil, titleColor: nil)
  235. rightButton.snp.makeConstraints { (make) in
  236. make.top.equalToSuperview().offset(kSafeStatusBarHeight)
  237. make.right.equalToSuperview()
  238. make.size.equalTo(44)
  239. }
  240. }
  241. func wr_setRightButton(image:UIImage) {
  242. wr_setRightButton(normal: image, highlighted: image, title: nil, titleColor: nil)
  243. rightButton.snp.makeConstraints { (make) in
  244. make.top.equalToSuperview().offset(kSafeStatusBarHeight)
  245. make.right.equalToSuperview()
  246. make.size.equalTo(44)
  247. }
  248. }
  249. func wr_setRightButton(title:String, titleColor:UIColor) {
  250. wr_setRightButton(normal: nil, highlighted: nil, title: title, titleColor: titleColor)
  251. rightButton.snp.makeConstraints { (make) in
  252. make.top.equalToSuperview().offset(kSafeStatusBarHeight)
  253. make.right.equalToSuperview().offset(-16)
  254. make.height.equalTo(44)
  255. }
  256. }
  257. // 左右按钮私有方法
  258. private func wr_setLeftButton(normal:UIImage?, highlighted:UIImage?, title:String?, titleColor:UIColor?) {
  259. leftButton.isHidden = false
  260. leftButton.setImage(normal, for: .normal)
  261. leftButton.setImage(highlighted, for: .highlighted)
  262. leftButton.setTitle(title, for: .normal)
  263. leftButton.setTitleColor(titleColor, for: .normal)
  264. }
  265. private func wr_setRightButton(normal:UIImage?, highlighted:UIImage?, title:String?, titleColor:UIColor?) {
  266. rightButton.isHidden = false
  267. rightButton.setImage(normal, for: .normal)
  268. rightButton.setImage(highlighted, for: .highlighted)
  269. rightButton.setTitle(title, for: .normal)
  270. rightButton.setTitleColor(titleColor, for: .normal)
  271. }
  272. }
  273. // MARK: - 导航栏左右按钮事件
  274. extension WRCustomNavigationBar
  275. {
  276. @objc func clickBack() {
  277. if let onClickBack = onClickLeftButton {
  278. onClickBack()
  279. }
  280. }
  281. @objc func clickRight() {
  282. if let onClickRight = onClickRightButton {
  283. onClickRight()
  284. }
  285. }
  286. }