-
Notifications
You must be signed in to change notification settings - Fork 38
/
DropDownMenuCell.swift
85 lines (66 loc) · 1.98 KB
/
DropDownMenuCell.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
Copyright (C) 2015 Quentin Mathe
Date: May 2015
License: MIT
*/
import UIKit
open class DropDownMenuCell : UITableViewCell {
open var customView: UIView? {
didSet {
guard let customView = customView else {
return
}
contentView.addSubview(customView)
}
}
/// For an app extension, the selector must take an argument.
open var menuAction: Selector?
/// For an app extension, the menu target must not be nil.
open weak var menuTarget: AnyObject?
open var showsCheckmark = true
open var rowHeight: CGFloat = 44
// MARK: - Initialization
override public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
fatalError("init(style:reuseIdentifier:) is not supported")
}
public init() {
super.init(style: .default, reuseIdentifier: NSStringFromClass(DropDownMenuCell.self))
setUp()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
open override func awakeFromNib() {
setUp()
}
private func setUp() {
textLabel?.text = "Untitled"
}
// MARK: - Layout
open var iconSize = CGSize(width: 24, height: 24)
override open func layoutSubviews() {
if let textLabel = textLabel {
if customView != nil && textLabel.text == nil {
textLabel.text = "Custom View Origin Hint"
}
textLabel.isHidden = customView != nil
}
super.layoutSubviews()
if let imageView = imageView, imageView.image != nil {
imageView.frame.size = iconSize
imageView.center = CGPoint(x: imageView.center.x, y: bounds.size.height / 2)
}
if let customView = customView {
if let textLabel = textLabel, imageView?.image != nil {
customView.frame.origin.x = textLabel.frame.origin.x
} else {
customView.center.x = bounds.width / 2
}
customView.center.y = bounds.height / 2
let margin: CGFloat = 5 // imageView?.frame.origin.x ?? 15
if customView.frame.maxX + margin > bounds.width {
customView.frame.size.width = bounds.width - customView.frame.origin.x - margin
}
}
}
}