在创建的时候选择
创建后结构
修改代码
MyPlayground
//: A UIKit based Playground for presenting user interfaceimport UIKit
import PlaygroundSupportlet vc = ViewController1()
let navigationVc = UINavigationController(rootViewController: vc)// Present the view controller in the Live View window
PlaygroundPage.current.liveView = navigationVc;
ViewController1
import Foundation
import UIKitpublic class ViewController1:UIViewController{public override func viewDidLoad(){super.viewDidLoad()// Do any additional setup after loading the view.let button = UIButton(type: .custom)button.frame=CGRect(x:0,y: 200,width: 300,height: 50)button.setTitle("点击跳转", for: .normal)button.setTitleColor(.white, for: .normal)button.backgroundColor = .bluebutton.addTarget(self, action: #selector(onButtonClick), for: .touchUpInside)view.addSubview(button)}@objc func onButtonClick() {navigationController?.pushViewController(ViewController2(), animated: true)}}
ViewController2
import Foundation
import UIKitpublic class ViewController2:UIViewController{public override func viewDidLoad(){super.viewDidLoad()let view = UIView()view.backgroundColor = .whitelet label = UILabel()label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)label.text = "Hello World!"label.textColor = .blackview.addSubview(label)self.view = view}
}