-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from hyperoslo/fix/bar
Use UINavigationBar
- Loading branch information
Showing
2 changed files
with
47 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,22 @@ | ||
import UIKit | ||
|
||
protocol HeaderViewDelegate: class { | ||
func headerViewDidPressClose(_ headerView: HeaderView) | ||
} | ||
|
||
/** | ||
Header view that simulates a navigation bar. | ||
*/ | ||
final class HeaderView: UIView { | ||
/// Title label. | ||
private lazy var label: UILabel = { | ||
/// UI elements on the navigation bar | ||
final class HeaderElement { | ||
static func makeLabel() -> UILabel { | ||
let label = UILabel() | ||
label.text = Title.text | ||
label.font = Title.font | ||
label.textColor = Title.color | ||
label.backgroundColor = Title.backgroundColor | ||
label.numberOfLines = 1 | ||
label.textAlignment = .center | ||
return label | ||
}() | ||
} | ||
|
||
/// Close button. | ||
private lazy var button: UIButton = { | ||
static func makeCloseButton() -> UIButton { | ||
let button = UIButton(type: .system) | ||
button.setTitle(CloseButton.text, for: UIControlState()) | ||
button.titleLabel?.font = CloseButton.font | ||
button.tintColor = CloseButton.color | ||
button.addTarget(self, action: #selector(buttonDidPress), for: .touchUpInside) | ||
return button | ||
}() | ||
|
||
/// Header view delegate. | ||
weak var delegate: HeaderViewDelegate? | ||
|
||
// MARK: - Initialization | ||
|
||
/** | ||
Creates a new instance of `HeaderView`. | ||
|
||
- Parameter frame: View frame. | ||
*/ | ||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
|
||
backgroundColor = Title.backgroundColor | ||
|
||
[label, button].forEach { | ||
addSubview($0) | ||
} | ||
} | ||
|
||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: - Layout | ||
|
||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
|
||
let insets = viewInsets | ||
let leadingPadding: CGFloat = 15 + insets.left | ||
let topPadding: CGFloat = 8 + insets.top | ||
let labelHeight: CGFloat = 40 | ||
|
||
button.sizeToFit() | ||
|
||
button.frame.origin = CGPoint( | ||
x: leadingPadding, | ||
y: ((frame.height - button.frame.height) / 2) + topPadding | ||
) | ||
|
||
label.frame = CGRect( | ||
x: 0, y: ((frame.height - labelHeight) / 2) + topPadding, | ||
width: frame.width, height: labelHeight | ||
) | ||
} | ||
|
||
// MARK: - Actions | ||
|
||
/** | ||
Close button action handler. | ||
*/ | ||
@objc private func buttonDidPress() { | ||
delegate?.headerViewDidPressClose(self) | ||
} | ||
} |