//
|
// YYViewController.swift
|
// YYBase
|
//
|
// Created by alvin_y on 2020/3/6.
|
// Copyright © 2020 yangwang. All rights reserved.
|
//
|
|
import UIKit
|
import RxCocoa
|
import RxSwift
|
import MBProgressHUD
|
import DZNEmptyDataSet
|
|
class YYViewController: UIViewController,UIGestureRecognizerDelegate {
|
// 调用此属性会自动拦截pop
|
open var yy_popBlock:(() -> Void)?
|
|
/// 导航栏黑线
|
var nav_bar_hair_line_imageView : UIImageView?
|
|
let disposeBag = DisposeBag()
|
|
/// MBProgressHUD
|
var hud : MBProgressHUD?
|
|
// 导航栏返回按钮图片
|
open var yy_nav_back_img:UIImage = UIImage.init(named: "icon_back") ?? UIImage.init() {
|
didSet {
|
if yy_isHiddenBackItem {
|
yy_isHiddenBackItem = true
|
} else {
|
let btn = navigationItem.leftBarButtonItem?.customView as! UIButton
|
btn.setImage(yy_nav_back_img, for: .normal)
|
}
|
}
|
}
|
|
// 是否需要返回按钮
|
open var yy_isHiddenBackItem:Bool = false {
|
didSet {
|
if yy_isHiddenBackItem {
|
navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "", style: .done, target: nil, action: nil)
|
} else {
|
navigationItem.leftBarButtonItem = UIBarButtonItem.yy_creat(image: yy_nav_back_img, target: self, alignment:.left, action: #selector(backItemEvent)).item
|
}
|
}
|
}
|
|
// 是否需要返回手势
|
open var yy_isEnabledBackTap:Bool = true {
|
didSet {
|
if yy_isEnabledBackTap {
|
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
|
} else {
|
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
|
}
|
}
|
}
|
|
/// 是否需要导航栏黑线
|
open var yy_isHiddenNavBarLine:Bool = true {
|
didSet {
|
if yy_isHiddenNavBarLine {
|
nav_bar_hair_line_imageView?.isHidden = true
|
} else {
|
nav_bar_hair_line_imageView?.isHidden = false
|
}
|
}
|
}
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
|
// Do any additional setup after loading the view.
|
yy_isHiddenBackItem = false
|
// 隐藏之后navigationBar不存在导致cash
|
if let nav = navigationController?.navigationBar{
|
nav_bar_hair_line_imageView = findHairlineImageViewUnder(view: nav)
|
}
|
view.backgroundColor = UIColor(hexString: "#F3F4F5")
|
|
|
self.navigationController?.navigationBar.isTranslucent = false
|
|
|
let attributes = [NSAttributedString.Key.foregroundColor:UIColor.color(light: UIColor.color(hexString: "#000000"), dark: UIColor.color(hexString: "#FFFFFF")),NSAttributedString.Key.font: UIFont.init(name: Medium, size: 18) ?? UIFont.systemFont(ofSize: 18)]
|
let bartinColor = UIColor.color(light: UIColor.color(hexString: "#FFFFFF"), dark: UIColor.color(hexString: "#191919"))
|
|
//修复导航栏问题
|
if #available(iOS 15.0, *) {
|
let bar = UINavigationBarAppearance()
|
bar.configureWithOpaqueBackground() //消除15的黑框
|
bar.backgroundEffect = nil
|
bar.shadowColor = nil
|
bar.titleTextAttributes = attributes
|
bar.backgroundColor = UIColor.white.withAlphaComponent(1)
|
navigationController?.navigationBar.scrollEdgeAppearance = bar //顶部透明
|
navigationController?.navigationBar.standardAppearance = bar
|
}else {
|
navigationController?.navigationBar.titleTextAttributes = attributes
|
navigationController?.navigationBar.barTintColor = bartinColor
|
}
|
navigationController?.navigationBar.isTranslucent = false
|
|
setupViews()
|
defineLayouts()
|
bindRx()
|
}
|
|
override func viewWillAppear(_ animated: Bool) {
|
super.viewWillAppear(animated)
|
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
|
|
}
|
|
//MARK: - 初始化或者配置所有的view,此方法将在UIViewController的viewDidLoad()方法中调用
|
func setupViews() {
|
|
}
|
|
//MARK: - 定义布局,在这里添加约束,此方法将在UIViewController的viewDidLoad()方法中调用
|
func defineLayouts() {
|
|
}
|
|
//MARK: - 绑定ViewModel中的数据,此方法将在UIViewController的viewDidLoad()方法中调用
|
func bindRx() {
|
|
}
|
|
//MARK: - NavigationController
|
// pop点击事件
|
@objc fileprivate func backItemEvent() {
|
|
// 拦截pop事件
|
if (yy_popBlock != nil) {
|
yy_popBlock?()
|
return
|
}
|
|
yy_pop()
|
}
|
|
/// UIStatusBarStyle
|
override var preferredStatusBarStyle: UIStatusBarStyle{
|
return .default
|
}
|
|
/// 找到导航栏的线条并选择显示或隐藏
|
/// - Parameter view: 导航栏View
|
func findHairlineImageViewUnder(view: UIView) -> UIImageView?{
|
if view.isKind(of: UIImageView.classForCoder()) && view.bounds.size.height <= 1.0{
|
return view as? UIImageView
|
}
|
for subView in view.subviews {
|
let imageView = self.findHairlineImageViewUnder(view: subView)
|
if (imageView != nil){
|
return imageView
|
}
|
}
|
return nil
|
}
|
|
|
/// pop
|
/// - Parameter animated: animated
|
open func yy_pop(_ animated:Bool = true) {
|
navigationController?.popViewController(animated: animated)
|
}
|
|
/// yy_popToRoot
|
/// - Parameter animated: animated
|
open func yy_popToRoot(_ animated: Bool = true) {
|
navigationController?.popToRootViewController(animated: animated)
|
}
|
|
open func yy_popToVC(index:Int) {
|
guard let vc = navigationController?.viewControllers[index] else {
|
return
|
}
|
navigationController?.popToViewController(vc, animated: true)
|
}
|
|
/// yy_push
|
/// - Parameters:
|
/// - vc: UIViewController
|
/// - animated: animated
|
/// - hiddenBottom: hiddenBottom
|
open func yy_push(vc:UIViewController, _ animated:Bool = true, _ hiddenBottom:Bool = true) {
|
vc.hidesBottomBarWhenPushed = hiddenBottom
|
self.navigationController?.pushViewController(vc, animated: animated)
|
}
|
|
/// push
|
///
|
/// - Parameters:
|
/// - vc: UIViewController
|
/// - animated: animated
|
/// - hiddenBottom: hiddenBottom
|
open func wy_pushAnimate(vc:UIViewController, _ animated:Bool = false, _ hiddenBottom:Bool = true){
|
let transition = CATransition()
|
transition.duration = 0.3
|
transition.type = .moveIn
|
transition.subtype = .fromTop
|
navigationController?.view.layer.add(transition, forKey: kCATransition)
|
vc.hidesBottomBarWhenPushed = hiddenBottom
|
navigationController?.pushViewController(vc, animated: animated)
|
}
|
|
|
/// yy_present
|
/// - Parameter vc: UIViewController
|
open func yy_present(vc:UIViewController){
|
self.present(vc, animated: true, completion: nil)
|
}
|
|
|
/// yy_present
|
/// - Parameter vc: UIViewController
|
open func yy_presentFullScreen(vc:UIViewController){
|
vc.modalPresentationStyle = .custom
|
vc.transitioningDelegate = self
|
vc.view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
|
self.present(vc, animated: true, completion: nil)
|
}
|
|
//MARK: - MBProgressHUD
|
/// 不带文字
|
func show() {
|
hud = build()
|
}
|
|
func hide() {
|
hud?.hide(animated: true)
|
}
|
|
private func build() -> MBProgressHUD {
|
let hud = MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
|
hud.mode = .indeterminate
|
hud.bezelView.style = .solidColor
|
hud.bezelView.color = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.7)
|
hud.customView?.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
|
return hud
|
}
|
}
|
|
|
// MARK: - UIViewControllerTransitioningDelegate
|
extension YYViewController:UIViewControllerTransitioningDelegate{
|
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
|
return DismissAnimation()
|
}
|
|
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
|
return PresentAnimation()
|
}
|
}
|
|
extension UIViewController {
|
|
/// 当前屏幕上的UIViewController
|
/// - Parameter base: Window
|
class func base(_ viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
|
if let nav = viewController as? UINavigationController {
|
return base(nav.visibleViewController)
|
}
|
if let tab = viewController as? UITabBarController {
|
return base(tab.selectedViewController)
|
}
|
if let presented = viewController?.presentedViewController {
|
return base(presented)
|
}
|
return viewController
|
}
|
}
|
// 把颜色转成图片
|
func imageFromColor(color: UIColor, viewSize: CGSize) -> UIImage{
|
|
let rect: CGRect = CGRect(x: 0, y: 0, width: viewSize.width, height: viewSize.height)
|
|
UIGraphicsBeginImageContext(rect.size)
|
|
let context: CGContext = UIGraphicsGetCurrentContext()!
|
|
context.setFillColor(color.cgColor)
|
|
context.fill(rect)
|
let image = UIGraphicsGetImageFromCurrentImageContext()
|
|
UIGraphicsGetCurrentContext()
|
|
return image!
|
|
}
|
|
|
extension YYViewController{
|
func aKeyLogin(){
|
|
}
|
}
|