Last active
October 28, 2019 18:26
-
-
Save florianldt/69d4e53d6ebf1507a15b26dba77894b9 to your computer and use it in GitHub Desktop.
self sizing modal with airbnb/AloeStackView - issue #41
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
// | |
// CustomView.swift | |
// AloeStackDemo | |
// | |
// Created by Florian LUDOT on 2/28/19. | |
// Copyright © 2019 Florian LUDOT. All rights reserved. | |
// | |
import UIKit | |
class CustomView: UIView { | |
let label: UILabel = { | |
let label = UILabel() | |
label.translatesAutoresizingMaskIntoConstraints = false | |
label.isUserInteractionEnabled = false | |
label.font = UIFont.boldSystemFont(ofSize: 20) | |
return label | |
}() | |
override init(frame: CGRect) { | |
super.init(frame: .zero) | |
self.backgroundColor = UIColor.random() | |
self.addSubview(label) | |
NSLayoutConstraint.activate([ | |
label.centerXAnchor.constraint(equalTo: self.centerXAnchor), | |
label.centerYAnchor.constraint(equalTo: self.centerYAnchor), | |
]) | |
NSLayoutConstraint.activate([ | |
self.heightAnchor.constraint(equalToConstant: 100) | |
]) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
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
// | |
// ModalPopupView.swift | |
// AloeStackDemo | |
// | |
// Created by Florian LUDOT on 2/28/19. | |
// Copyright © 2019 Florian LUDOT. All rights reserved. | |
// | |
import UIKit | |
import AloeStackView | |
class ModalPopupView: UIView { | |
var aloeStackViewHeightConstraint: NSLayoutConstraint! | |
let maxHeight: CGFloat = UIScreen.main.bounds.height - 200 | |
let aloeStackView: AloeStackView = { | |
let sv = AloeStackView() | |
sv.translatesAutoresizingMaskIntoConstraints = false | |
sv.rowInset = .zero | |
sv.separatorHeight = 0 | |
sv.backgroundColor = .clear | |
return sv | |
}() | |
override init(frame: CGRect) { | |
super.init(frame: .zero) | |
self.translatesAutoresizingMaskIntoConstraints = false | |
self.addSubview(aloeStackView) | |
aloeStackViewHeightConstraint = aloeStackView.heightAnchor.constraint(equalToConstant: 10) | |
aloeStackViewHeightConstraint.isActive = true | |
NSLayoutConstraint.activate([ | |
aloeStackView.widthAnchor.constraint(equalToConstant: 260), | |
]) | |
NSLayoutConstraint.activate([ | |
self.topAnchor.constraint(equalTo: aloeStackView.topAnchor, constant: 0), | |
self.leftAnchor.constraint(equalTo: aloeStackView.leftAnchor, constant: 0), | |
self.rightAnchor.constraint(equalTo: aloeStackView.rightAnchor, constant: 0), | |
self.bottomAnchor.constraint(equalTo: aloeStackView.bottomAnchor, constant: 0), | |
]) | |
setupViews() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
private func setupViews() { | |
let view100 = CustomView() | |
view100.label.text = "One row..." | |
aloeStackView.addRow(view100) | |
} | |
func resize() { | |
aloeStackViewHeightConstraint.constant = min(aloeStackView.contentSize.height, maxHeight) | |
} | |
func push() { | |
let view100 = CustomView() | |
view100.label.text = "...another row" | |
aloeStackView.addRow(view100, animated: true) | |
} | |
func pop() { | |
guard let lastRow = aloeStackView.getAllRows().last else { return } | |
aloeStackView.removeRow(lastRow, animated: true) | |
} | |
} |
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
// | |
// ModelPopUpViewController.swift | |
// AloeStackDemo | |
// | |
// Created by Florian LUDOT on 2/28/19. | |
// Copyright © 2019 Florian LUDOT. All rights reserved. | |
// | |
import UIKit | |
class ModalPopoUpViewController: UIViewController { | |
let modalView = ModalPopupView() | |
var numberOfViewsInModal: Double = 1 | |
let stepper: UIStepper = { | |
let s = UIStepper() | |
s.translatesAutoresizingMaskIntoConstraints = false | |
s.minimumValue = 1 | |
s.maximumValue = 10 | |
s.stepValue = 1 | |
s.addTarget(self, action: #selector(valueChanged), for: .valueChanged) | |
return s | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .lightGray | |
view.addSubview(modalView) | |
view.addSubview(stepper) | |
NSLayoutConstraint.activate([ | |
modalView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0), | |
modalView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0) | |
]) | |
NSLayoutConstraint.activate([ | |
stepper.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0), | |
stepper.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0) | |
]) | |
} | |
override func viewDidLayoutSubviews() { | |
super.viewDidLayoutSubviews() | |
modalView.resize() | |
} | |
@objc private func valueChanged(stepper: UIStepper) { | |
stepper.value > numberOfViewsInModal ? modalView.push() : modalView.pop() | |
self.numberOfViewsInModal = stepper.value | |
} | |
} |
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
// | |
// UIColor+Extension.swift | |
// AloeStackDemo | |
// | |
// Created by Florian LUDOT on 2/28/19. | |
// Copyright © 2019 Florian LUDOT. All rights reserved. | |
// | |
import UIKit | |
extension UIColor { | |
static func random(withAlpha alpha: CGFloat = 1.0) -> UIColor { | |
let r = CGFloat.random(in: 0...1) | |
let g = CGFloat.random(in: 0...1) | |
let b = CGFloat.random(in: 0...1) | |
return UIColor(red: r, green: g, blue: b, alpha: alpha) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment