diff options
author | Eric Kuck <eric@bluelinelabs.com> | 2018-12-14 17:12:59 -0600 |
---|---|---|
committer | Eric Kuck <eric@bluelinelabs.com> | 2018-12-14 17:15:22 -0600 |
commit | 7a24f18eb753180800f9b44a767b0d59e4e702b7 (patch) | |
tree | 0e02b3ff59a672b0b25eb7bcc195e23730abec40 /WireGuard/WireGuard/UI/iOS/SharedViews/ButtonCell.swift | |
parent | Prettier log time format (diff) | |
download | wireguard-apple-7a24f18eb753180800f9b44a767b0d59e4e702b7.tar.xz wireguard-apple-7a24f18eb753180800f9b44a767b0d59e4e702b7.zip |
Most similar views now shared between ViewControllers
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
Diffstat (limited to 'WireGuard/WireGuard/UI/iOS/SharedViews/ButtonCell.swift')
-rw-r--r-- | WireGuard/WireGuard/UI/iOS/SharedViews/ButtonCell.swift | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/SharedViews/ButtonCell.swift b/WireGuard/WireGuard/UI/iOS/SharedViews/ButtonCell.swift new file mode 100644 index 0000000..4702993 --- /dev/null +++ b/WireGuard/WireGuard/UI/iOS/SharedViews/ButtonCell.swift @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import UIKit + +class ButtonCell: UITableViewCell { + var buttonText: String { + get { return button.title(for: .normal) ?? "" } + set(value) { button.setTitle(value, for: .normal) } + } + var hasDestructiveAction: Bool { + get { return button.tintColor == .red } + set(value) { button.tintColor = value ? .red : buttonStandardTintColor } + } + var onTapped: (() -> Void)? + + let button: UIButton = { + let button = UIButton(type: .system) + button.titleLabel?.font = UIFont.preferredFont(forTextStyle: .body) + button.titleLabel?.adjustsFontForContentSizeCategory = true + return button + }() + + var buttonStandardTintColor: UIColor + + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { + buttonStandardTintColor = button.tintColor + super.init(style: style, reuseIdentifier: reuseIdentifier) + + contentView.addSubview(button) + button.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + button.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor), + contentView.layoutMarginsGuide.bottomAnchor.constraint(equalTo: button.bottomAnchor), + button.centerXAnchor.constraint(equalTo: contentView.centerXAnchor) + ]) + + button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) + } + + @objc func buttonTapped() { + onTapped?() + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func prepareForReuse() { + super.prepareForReuse() + buttonText = "" + onTapped = nil + hasDestructiveAction = false + } +} |