|
//
// FFSheetView.swift
// FFAlert
//
// Created by FFIB on 2017/11/17.
// Copyright © 2017年 FFIB. All rights reserved.
//
import UIKit
class FFSheetView: UIView, FFAlertRepresentable {
var contentView: UIView?
var contentHeight: CGFloat = 0
var tableView = UITableView()
var contentEdgInsets = UIEdgeInsets(top: 0, left: 6, bottom: 0, right: 6)
var title: NSAttributedString?
var message: NSAttributedString?
var edgInsets: (left: CGFloat, right: CGFloat) = (0, 0)
let separactorColor = UIColor(red: 226 / 255, green: 226 / 255, blue: 226 / 255, alpha: 1)
let separactorHeight: CGFloat = 6
var alertActions = [FFAlertAction]()
var titleLabel = FFAlertLabel()
var messageLabel = FFAlertLabel()
private let labelLeading: CGFloat = 12
private let labelTrailing: CGFloat = 12
private var titleTop: CGFloat = 10
private var messageTop: CGFloat = 14
private var isNeedlayout = true
override func layoutSubviews() {
super.layoutSubviews()
if isNeedlayout {
configurationOfTitle()
configurationOfMessage()
configurationOfContentView()
configurationOfTableView()
isNeedlayout = false
}
}
private func configurationOfContentView() {
if contentView == nil {
contentView = UIView()
}
addSubview(contentView!)
contentView!.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(
[NSLayoutConstraint(item: contentView!, attribute: .leading,
relatedBy: .equal,
toItem: self, attribute: .leading,
multiplier: 1, constant: contentEdgInsets.left),
NSLayoutConstraint(item: contentView!, attribute: .trailing,
relatedBy: .equal,
toItem: self, attribute: .trailing,
multiplier: 1, constant: -contentEdgInsets.right),
NSLayoutConstraint(item: contentView!, attribute: .top,
relatedBy: .equal,
toItem: messageLabel, attribute: .bottom,
multiplier: 1, constant: contentEdgInsets.top),
NSLayoutConstraint(item: contentView!, attribute: .height,
relatedBy: .equal,
toItem: nil, attribute: .height,
multiplier: 1, constant: contentHeight)]
)
}
private func configurationOfTableView() {
addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .none
tableView.register(FFSheetActionCell.classForCoder(), forCellReuseIdentifier: "FFSheetViewCell")
NSLayoutConstraint.activate(
[NSLayoutConstraint(item: tableView, attribute: .leading,
relatedBy: .equal,
toItem: self, attribute: .leading,
multiplier: 1, constant: 0),
NSLayoutConstraint(item: tableView, attribute: .trailing,
relatedBy: .equal,
toItem: self, attribute: .trailing,
multiplier: 1, constant: 0),
NSLayoutConstraint(item: tableView, attribute: .top,
relatedBy: .equal,
toItem: contentView, attribute: .bottom,
multiplier: 1, constant: 0),
NSLayoutConstraint(item: tableView, attribute: .bottom,
relatedBy: .equal,
toItem: self, attribute: .bottom,
multiplier: 1, constant: 0),
NSLayoutConstraint(item: tableView, attribute: .height,
relatedBy: .equal,
toItem: nil, attribute: .height,
multiplier: 1, constant: CGFloat(alertActions.count * 44))]
)
}
private func configurationOfTitle() {
addSubview(titleLabel)
titleLabel.attributedText = title
titleTop = title!.string.isEmpty ? 0 : titleTop
titleLabel.sizeToFit()
NSLayoutConstraint.activate(
[NSLayoutConstraint(item: titleLabel, attribute: .leading,
relatedBy: .equal,
toItem: self, attribute: .leading,
multiplier: 1, constant: labelLeading),
NSLayoutConstraint(item: titleLabel, attribute: .trailing,
relatedBy: .equal,
toItem: self, attribute: .trailing,
multiplier: 1, constant: -labelTrailing),
NSLayoutConstraint(item: titleLabel, attribute: .top,
relatedBy: .equal,
toItem: self, attribute: .top,
multiplier: 1, constant: titleTop)]
)
}
private func configurationOfMessage() {
addSubview(messageLabel)
messageLabel.attributedText = message
messageTop = message!.string.isEmpty ? 0 : messageTop
messageLabel.sizeToFit()
NSLayoutConstraint.activate(
[NSLayoutConstraint(item: messageLabel, attribute: .leading,
relatedBy: .equal,
toItem: self, attribute: .leading,
multiplier: 1, constant: labelLeading),
NSLayoutConstraint(item: messageLabel, attribute: .trailing,
relatedBy: .equal,
toItem: self, attribute: .trailing,
multiplier: 1, constant: -labelTrailing),
NSLayoutConstraint(item: messageLabel, attribute: .top,
relatedBy: .equal,
toItem: titleLabel, attribute: .bottom,
multiplier: 1, constant: messageTop)]
)
}
}
extension FFSheetView: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == alertActions.count - 1 {
return 44
}
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var r = next
while r != nil {
if let ctl = r as? UIViewController, ctl.isKind(of: FFAlertController.classForCoder()) {
ctl.dismiss(animated: true, completion: nil)
}
r = r?.next
}
let alertAction = alertActions[indexPath.row]
if let handler = alertAction.handler {
handler(alertAction)
}
}
}
extension FFSheetView: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return alertActions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FFSheetViewCell", for: indexPath) as! FFSheetActionCell
let alertAction = alertActions[indexPath.row]
if indexPath.row < alertActions.count - 1 {
let edgInsets = alertAction.contentEdgInsets ?? UIEdgeInsets.zero
alertAction.contentEdgInsets?.bottom = edgInsets.bottom - separactorHeight
}
cell.setInfo(alertAction: alertAction)
cell.backgroundColor = separactorColor
return cell
}
}
|