2// BaseViewController.swift
5// Created by cstore on 19/10/2019.
6// Copyright © 2019 Daria Rednikina. All rights reserved.
11class BaseViewController: UIViewController {
15 var bottomConstraint: NSLayoutConstraint?
19 func addStackViewWithButtons(leftBtn: Button? = nil,
20 rightBtn: Button? = nil,
21 constant: CGFloat = -20) {
23 let stackView = UIStackView()
24 stackView.translatesAutoresizingMaskIntoConstraints = false
25 stackView.backgroundColor = .red
26 view.addSubview(stackView)
28 leftBtn?.translatesAutoresizingMaskIntoConstraints = false
29 rightBtn?.translatesAutoresizingMaskIntoConstraints = false
31 bottomConstraint = NSLayoutConstraint(item: stackView,
34 toItem: view.safeAreaLayoutGuide,
39 guard let bottomContraint = bottomConstraint else { return }
40 view.addConstraint(bottomContraint)
42 NSLayoutConstraint.activate([
43 stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
44 stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
45 stackView.heightAnchor.constraint(equalToConstant: 40)
48 switch (leftBtn, rightBtn) {
49 case (.some(let leftButton), .none):
50 setUpLeftButton(leftButton, stackView)
51 case (.none, .some(let rigthButton)):
52 setUpRightButton(rigthButton, stackView)
53 case (.some(let leftButton), .some(let rightButton)):
54 setUpTwoButtons(leftButton, rightButton, stackView)
62 private func setUpTwoButtons(_ leftBtn: UIButton, _ rightBtn: UIButton, _ stackView: UIStackView) {
63 NSLayoutConstraint.activate([
64 leftBtn.widthAnchor.constraint(equalToConstant: 155),
65 rightBtn.widthAnchor.constraint(equalToConstant: 155)
68 stackView.addArrangedSubview(leftBtn)
69 stackView.addArrangedSubview(rightBtn)
71 stackView.distribution = .equalSpacing
74 private func setUpLeftButton(_ leftBtn: UIButton, _ stackView: UIStackView) {
75 leftBtn.widthAnchor.constraint(equalToConstant: 155).isActive = true
77 stackView.addArrangedSubview(leftBtn)
78 stackView.alignment = .trailing
81 private func setUpRightButton(_ rightBtn: UIButton, _ stackView: UIStackView) {
82 rightBtn.widthAnchor.constraint(equalToConstant: 155).isActive = true
84 stackView.addArrangedSubview(rightBtn)
86 stackView.axis = .vertical
87 stackView.alignment = .trailing
88 stackView.distribution = .fillProportionally
93 func setUpKeyboard() {
94 NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotifications), name: UIResponder.keyboardWillShowNotification, object: nil)
96 NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotifications), name: UIResponder.keyboardDidHideNotification, object: nil)
100 func handleKeyboardNotifications(notification: NSNotification){
101 if let userInfo = notification.userInfo {
102 let keyBoardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
103 bottomConstraint?.constant = notification.name == UIResponder.keyboardWillShowNotification ? -keyBoardFrame.height + view.safeAreaInsets.bottom - 20 : 0
105 UIView.animate(withDuration: 0, delay: 0, options: .curveEaseOut, animations: {
106 self.view.layoutIfNeeded()
113 func displayAlert(forError error: GeneralError = GeneralError.noData, with message: String? = nil) {
114 var title: String? = message == nil ? error.localizedDescription : message
116 case .serverError(let se):
118 fallthrough // да тоже произведение искусства
120 let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert)
121 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
122 self.present(alert, animated: true, completion: nil)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list