我对swift和iOS相对较新,无法找到答案或帮助解决我的问题.我想在单击上一个视图中的按钮时创建导航控制器视图.我之前的观点是在最后一页上有一个按钮.我成功地将按钮连接到我的下一个视图,但我无法使视图像导航控制器一样.例如,当显示导航栏时,我无法向其添加导航项.
所以我的问题是:有没有办法设置我创建的新视图,方法是在第一个视图中单击我的按钮作为导航视图的根控制器?
我的代码的简短版本:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: view.frame.width / 2, y: view.frame.height / 2, width: 40, height: 40))
button.backgroundColor = .red
button.tintColor = .white
button.setTitle("Test", for: .normal)
button.addTarget(self, action: #selector(change), for: .touchUpInside)
view.addSubview(button)
}
func change () {
let otherView = SecondViewController()
self.present(otherView, animated: true, completion: nil)
}
}
class SecondViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
}
}
Run Code Online (Sandbox Code Playgroud)
}
class ViewController: UIViewController {
override func …Run Code Online (Sandbox Code Playgroud) uinavigationbar uiviewcontroller uinavigationcontroller ios swift