废话不说, 上图看效果
![](https://img.haomeiwen.com/i1874747/2978b9a96ba07073.png)
实现思路:
为UIScrollVIew写一个分类, 在分类里提供显示和隐藏空白页占位图的功能, 并提供文本点击回调的方法
全部代码:
// Created by 李大宽 on 2017/11/20.
// Copyright © 2017年 李大宽. All rights reserved.
//
import UIKit
// 点击代理
protocol EmptyViewDelegate: NSObjectProtocol {
/** 点击了文本 */
func didClickedText()
}
private var empty_component_key: UInt8 = 25
private var delegate_key: UInt8 = 98
extension UIScrollView {
// component view
var component: EmptyComponent? {
set {
objc_setAssociatedObject(self, &empty_component_key, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
return (objc_getAssociatedObject(self, &empty_component_key) as? EmptyComponent)
}
}
// delegate
weak var empty_delegate: EmptyViewDelegate? {
set {
objc_setAssociatedObject(self, &delegate_key, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
}
get {
return objc_getAssociatedObject(self, &delegate_key) as? EmptyViewDelegate
}
}
/** image 提示图片(可不填) info 提示信息(可不填)*/
public func configEmptyPage(image: UIImage?=nil,
info: String?=nil) {
if component == nil {
component = EmptyComponent(frame: self.bounds)
if component?.superview == nil {
addSubview(component!)
bringSubview(toFront: component!)
}
}
component?.isHidden = true
component?.info_button.addTarget(self, action: #selector(info_button_action(sender:)), for: .touchUpInside)
component?.backgroundColor = backgroundColor
component?.note_pic.image = image
component?.info_button.setTitle(info, for: .normal)
component?.resetConstraints()
}
// 显示
public func showEmptyPage(){
component?.isHidden = false
self.isScrollEnabled = false
}
// 隐藏
public func dismissEmptyPage() {
component?.isHidden = true
self.isScrollEnabled = true
}
///
/// delegate action
@objc func info_button_action(sender: UIButton) {
empty_delegate?.didClickedText()
}
}
// 内容视图
class EmptyComponent: UIView {
// 设置控件大小
fileprivate func resetConstraints() {
// constraints
if note_pic.image == nil {
note_pic.bounds = CGRect.zero
} else if note_pic.image?.size != CGSize.zero {
note_pic.bounds = CGRect.init(origin: CGPoint.zero, size: (note_pic.image?.size)!)
}
note_pic.center = CGPoint.init(x: frame.midX, y: frame.midY-100)
info_button.bounds = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: frame.maxX, height: 20))
info_button.center = CGPoint.init(x: frame.midX, y: note_pic.frame.maxY+20)
}
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(note_pic)
addSubview(info_button)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// 提示图片
lazy var note_pic: UIImageView = {
let note = UIImageView()
return note
}()
// 按钮
lazy var info_button: UIButton = {
let button = UIButton(type: .custom)
button.setTitleColor(#colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1), for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
return button
}()
}
真实使用案例:
// view did appear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if dataArr.isEmpty {
startNetwork()
}
// empty view
tableView.configEmptyPage(image: #imageLiteral(resourceName: "empty_placeholder_net_error.png"), info: "网络故障 点击重试")
tableView.empty_delegate = self
// 在网络请求失败的地方写上这句, 显示空白页
tableView.showEmptyPage()
}
func didClickedText() {
tableView.dismissEmptyPage()
}
代码总量总共就几十行, 非常简单快捷
如需源码可留下邮箱.
网友评论