在实际开发中很容易遇到对空界面的使用,怎么一劳永逸的使用空界面呢!!!
本次给大家带来的是给UIViewController的一个拓展,废话不说直接上代码,相信看完会有新的认识
//
// YMEmptyView.swift
// MamElectric
//
// Created by Tony on 2018/8/18.
// Copyright © 2018年 Tony. All rights reserved.
//
import UIKit
let EmptyViewNetwork = "EmptyViewNetwork" // 网络不好
let EmptyViewError = "EmptyViewError" // 网络不好
let EmptyViewNoData = "EmptyNoData" // 没有数据
let EmptyViewNoLogin = "EmptyNoLogin" // 没有登录
let EmptyViewLayoutKeyIcon = "icon";
let EmptyViewLayoutKeyMainTitle = "mainTitle";
let EmptyViewLayoutKeySecondTitle = "secondTitle";
let EmptyViewLayoutKeyButtonTitle = "buttonTitle";
let EmptyViewActionBtn = "Button" //点击事件的来源Btn
let EmptyViewActionTapGesture = "TapGesture" //点击事件的来源TapGesture
var EmptyViewLayoutDictionary : [String : [String:Any]]?
class YMEmptyView: UIView {
// 头像
var iconImageView : UIImageView!
// 主要Label
var mainTitleLabel : UILabel!
// 第二个Label
var SecondTitle : UILabel!
/// 点击按钮
var actionButton : UIButton!
/// 空界面的类型
var emptyViewType : String! {
didSet{
analysisEmptyViewType()
}
}
// logo
var iconImage : UIImage! {
didSet{
analysisIconImage()
}
}
// 主要LabelText
var mainTitle : String! {
didSet{
analysisMainTitle()
}
}
// secondTitle
var secondTitle : String! {
didSet{
analysisSecondTitle()
}
}
/// 按钮上title
var btnTitle : String! {
didSet{
analysisBtnTitle()
}
}
// 点击时间
var actionBlockc : ((_ emptyViewType:String,_ actionSource:String)->())!
init(frame: CGRect, actionBlock : @escaping (_ emptyViewType:String,_ actionSource:String)->()) {
super.init(frame: frame)
self.backgroundColor = UIColor.groupTableViewBackground
creatView()
actionBlockc = actionBlock
}
func creatView(){
iconImageView = UIImageView()
self.addSubview(iconImageView)
iconImageView.snp.makeConstraints { (make) in
make.width.equalTo(100)
make.height.equalTo(100)
make.centerX.equalTo(self.snp.centerX)
make.centerY.equalTo(self.snp.centerY).offset(-50)
}
mainTitleLabel = UILabel()
self.addSubview(mainTitleLabel)
mainTitleLabel.font = UIFont.systemFont(ofSize: 14)
mainTitleLabel.textColor = UIConfigure.LightGrayColor
mainTitleLabel.snp.makeConstraints { (make) in
make.centerX.equalTo(self.snp.centerX)
make.top.equalTo(iconImageView.snp.bottom).offset(10)
}
SecondTitle = UILabel()
self.addSubview(SecondTitle)
SecondTitle.numberOfLines = 0
SecondTitle.textAlignment = .center
SecondTitle.font = UIFont.systemFont(ofSize: 14)
SecondTitle.textColor = UIConfigure.LightGrayColor
SecondTitle.snp.makeConstraints { (make) in
make.centerX.equalTo(self.snp.centerX)
make.top.equalTo(mainTitleLabel.snp.bottom).offset(10)
}
actionButton = UIButton()
actionButton.isHidden = true
self.addSubview(actionButton)
actionButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
actionButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
actionButton.backgroundColor = UIColor(red: 50/255, green: 165/255, blue: 70/255, alpha: 1)
actionButton.layer.cornerRadius = 4
actionButton.layer.masksToBounds = true
actionButton.addTarget(self, action: #selector(actionBtn), for: UIControl.Event.touchUpInside)
actionButton.snp.makeConstraints { (make) in
make.centerX.equalTo(self.snp.centerX)
make.top.equalTo(SecondTitle.snp.bottom).offset(10)
make.width.equalTo(200)
}
let tap = UITapGestureRecognizer(target: self, action: #selector(actionTap(sender:)))
tap.numberOfTouchesRequired = 1
tap.numberOfTapsRequired = 1
self.addGestureRecognizer(tap)
}
@objc func actionBtn(){
self.actionBlockc(emptyViewType,EmptyViewActionBtn)
}
@objc func actionTap(sender : UITapGestureRecognizer){
self.actionBlockc(emptyViewType,EmptyViewActionTapGesture)
}
class func emptyViewLayouts()-> [String : [String:Any]] {
if EmptyViewLayoutDictionary == nil {
EmptyViewLayoutDictionary = [
EmptyViewNetwork: [
EmptyViewLayoutKeyIcon:BaseImage(named: "empty_network")?.image! as Any,
EmptyViewLayoutKeyMainTitle:"网络不给力",
EmptyViewLayoutKeySecondTitle:"建议检查网络后\r\n点击屏幕尝试重新连接",
EmptyViewLayoutKeyButtonTitle:""
],
EmptyViewError: [
EmptyViewLayoutKeyIcon:BaseImage(named: "empty_default")?.image! as Any,
EmptyViewLayoutKeyMainTitle:"出错了",
EmptyViewLayoutKeySecondTitle:"",
EmptyViewLayoutKeyButtonTitle:""
],
EmptyViewNoData: [
EmptyViewLayoutKeyIcon:BaseImage(named: "no_data")?.image! as Any,
EmptyViewLayoutKeyMainTitle:"暂无内容",
EmptyViewLayoutKeySecondTitle:"点击屏幕尝试重新刷新",
EmptyViewLayoutKeyButtonTitle:""
],
EmptyViewNoLogin:[
EmptyViewLayoutKeyIcon:BaseImage(named: "QSD_Empty_UseCarInfo")?.image! as Any,
EmptyViewLayoutKeyMainTitle:"当前未登录",
EmptyViewLayoutKeySecondTitle:"点击登录,查看您的订单信息",
EmptyViewLayoutKeyButtonTitle:"去登录"
]
]
}
return EmptyViewLayoutDictionary!
}
// 解析空界面
func analysisEmptyViewType(){
let layout = YMEmptyView.emptyViewLayouts()[emptyViewType]!
self.iconImage = layout[EmptyViewLayoutKeyIcon] as? UIImage
self.mainTitle = layout[EmptyViewLayoutKeyMainTitle] as? String
self.secondTitle = layout[EmptyViewLayoutKeySecondTitle] as? String
self.btnTitle = layout[EmptyViewLayoutKeyButtonTitle] as? String
}
func analysisIconImage(){
self.iconImageView.image = iconImage
}
func analysisMainTitle(){
self.mainTitleLabel.text = mainTitle
}
func analysisSecondTitle(){
self.SecondTitle.text = secondTitle
}
func analysisBtnTitle(){
self.actionButton.setTitle(btnTitle, for: UIControl.State.normal)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
var commonParametersBinddd = "commonParametersBinddd"
extension UIViewController {
var emptyView : YMEmptyView!{
get{
if objc_getAssociatedObject(self, &commonParametersBinddd) == nil {
let emptyViewN = YMEmptyView(frame: CGRect.zero) { [weak self](emptyViewType, actionSource) in
self?.onEmptyViewResponded(emptyViewType: emptyViewType, actionSource: actionSource)
}
objc_setAssociatedObject(self, &commonParametersBinddd, emptyViewN, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return emptyViewN
}else{
let emptyViewN = objc_getAssociatedObject(self, &commonParametersBinddd) as! YMEmptyView
emptyViewN.actionBlockc = { [weak self](emptyViewType, actionSource) in
self?.onEmptyViewResponded(emptyViewType: emptyViewType, actionSource: actionSource)
}
return objc_getAssociatedObject(self, &commonParametersBinddd) as? YMEmptyView
}
}
set{
objc_setAssociatedObject(self, &commonParametersBinddd, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
func showEmptyView()->YMEmptyView {
self.emptyView.frame = self.view.bounds
if self.navigationController != nil && self.navigationController?.topViewController == self && self.parent == self.navigationController && (self.edgesForExtendedLayout == UIRectEdge.top) {
let statusBarAndNavigationBarHeight = CGFloat(64.0)
self.emptyView.frame.y += statusBarAndNavigationBarHeight
self.emptyView.frame.height -= statusBarAndNavigationBarHeight
self.emptyView.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth,UIView.AutoresizingMask.flexibleHeight]
self.view.addSubview(self.emptyView)
}else{
let d = 44 + UIApplication.shared.statusBarFrame.size.height
self.emptyView.frame.y += d
self.emptyView.frame.height -= d
self.emptyView.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth,UIView.AutoresizingMask.flexibleHeight]
self.view.addSubview(self.emptyView)
}
return self.emptyView
}
// 显示空界面
func showEmptyViewForType(type : String) -> YMEmptyView{
let emptyView = self.showEmptyView()
emptyView.emptyViewType = type
if type == EmptyViewNoLogin {
emptyView.actionButton.isHidden = false
}else{
emptyView.actionButton.isHidden = true
}
return emptyView
}
// func showEmptyViewForError(){
// self.emptyView.emptyViewType = EmptyViewError
// self.emptyView
// }
// 点击空白界面
@objc func onEmptyViewResponded(emptyViewType:String, actionSource:String){
// DO Nothing
}
func hideEmptyView(){
self.emptyView.removeFromSuperview()
}
}