Created
May 12, 2020 08:09
-
-
Save stevencurtis/ddb78d673e2df4d8c4b4c024bd42d404 to your computer and use it in GitHub Desktop.
loadViewExample
This file contains hidden or 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
| import UIKit | |
| import PlaygroundSupport | |
| final class MyViewController: UIViewController { | |
| override func loadView() { | |
| let view = ButtonView() | |
| view.button.addTarget(self, action: #selector(buttonDidTap), for: .touchDown) | |
| self.view = view | |
| } | |
| @objc | |
| private func buttonDidTap() { | |
| let controller = DetailViewController() | |
| present(controller, animated: true, completion: nil) | |
| } | |
| } | |
| class ButtonView: UIView { | |
| let button = UIButton(type: .custom) | |
| override init(frame: CGRect) { | |
| super.init(frame: frame) | |
| createSubviews() | |
| } | |
| required init?(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| createSubviews() | |
| } | |
| func createSubviews() { | |
| self.backgroundColor = .green | |
| button.frame = CGRect(x: 0, y: 0, width: 200, height: 20) | |
| button.setTitle("Tap to go to Detail View", for: .normal) | |
| button.setTitleColor(.black, for: .normal) | |
| button.isUserInteractionEnabled = true | |
| self.addSubview(button) | |
| button.translatesAutoresizingMaskIntoConstraints = false | |
| button.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true | |
| button.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true | |
| } | |
| } | |
| final class DetailViewController: UIViewController { | |
| override func loadView() { | |
| let view = DetailUIView() | |
| view.button.addTarget(self, action: #selector(buttonDidTap), for: .touchDown) | |
| self.view = view | |
| } | |
| @objc | |
| private func buttonDidTap() { | |
| dismiss(animated: true, completion: nil) | |
| } | |
| } | |
| class DetailUIView: UIView { | |
| let button = UIButton(type: .custom) | |
| override init(frame: CGRect) { | |
| super.init(frame: frame) | |
| createSubviews() | |
| } | |
| required init?(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| createSubviews() | |
| } | |
| func createSubviews() { | |
| self.backgroundColor = .orange | |
| button.frame = CGRect(x: 0, y: 0, width: 200, height: 20) | |
| button.setTitle("Tap to dismiss view", for: .normal) | |
| button.setTitleColor(.black, for: .normal) | |
| button.isUserInteractionEnabled = true | |
| self.addSubview(button) | |
| button.translatesAutoresizingMaskIntoConstraints = false | |
| button.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true | |
| button.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true | |
| self.addSubview(button) | |
| } | |
| } | |
| // set the view and indefiniteexecution | |
| PlaygroundPage.current.needsIndefiniteExecution = true | |
| PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment