Skip to content

Instantly share code, notes, and snippets.

@BrandonShega
Last active June 22, 2017 13:52
Show Gist options
  • Save BrandonShega/35975e2151c022308f53b37b66757647 to your computer and use it in GitHub Desktop.
Save BrandonShega/35975e2151c022308f53b37b66757647 to your computer and use it in GitHub Desktop.
Transition with completion
//: Playground - noun: a place where people can play
import UIKit
import XCPlayground
import PlaygroundSupport
extension UINavigationController {
func popViewController(completion: @escaping () -> ()) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
popViewController(animated: true)
CATransaction.commit()
}
}
class MyNavController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
}
}
class MyViewController: UIViewController {
lazy var button: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 50, width: 100, height: 50))
button.backgroundColor = .blue
button.setTitle("Tap Me", for: .normal)
button.setTitleColor(.white, for: .normal)
button.addTarget(self, action: #selector(pop), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(button)
}
func pop() {
navigationController?.popViewController() {
print("popped")
}
}
}
class MyOtherViewController: UIViewController {
lazy var button: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 50, width: 100, height: 50))
button.backgroundColor = .blue
button.setTitle("Tap Me", for: .normal)
button.setTitleColor(.white, for: .normal)
button.addTarget(self, action: #selector(push), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(button)
}
func push() {
let vc = MyViewController()
navigationController?.pushViewController(vc, animated: true)
print("pushed")
}
}
let vc = MyOtherViewController()
let navController = MyNavController(rootViewController: vc)
//let myvc = MyViewController()
//navController.pushViewController(myvc, animated: true)
PlaygroundPage.current.liveView = navController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment