【发布时间】:2015-12-14 05:22:30
【问题描述】:
我正在尝试以编程方式创建一个导航控制器,该控制器通过点击按钮转换到另一个视图。在当前状态下,当点击按钮时,第二个视图被放置在第一个视图的“后面”,没有水平过渡。由于此时我仍然可以看到该按钮,如果我再次点击该按钮,水平过渡现在可以工作,但第二个视图仍位于第一个视图的后面。谁能告诉我我错过了什么?
谢谢!
ViewController.swift
import UIKit
class ViewController: UINavigationController {
var btn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.whiteColor()
createUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func createUI() {
btn = UIButton(frame: CGRect(x: 10, y: 100, width: 100, height: 50))
btn.backgroundColor = UIColor.orangeColor()
btn.setTitle("GO", forState: UIControlState.Normal)
btn.addTarget(self, action: "go", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(btn)
}
func go() {
println("go")
var view = SecondViewController()
self.pushViewController(view, animated: true)
}
}
SecondViewController.swift
import UIKit
class SecondViewController: UIViewController {
var lbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.redColor()
createUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func createUI() {
lbl = UILabel(frame: CGRect(x: 50, y: 200, width: 200, height: 50))
lbl.backgroundColor = UIColor.greenColor()
lbl.text = "LABEL"
self.view.addSubview(lbl)
}
}
【问题讨论】:
-
看起来你的“ViewController.swift”实际上是 UINavigationController - 它不应该有任何视图。您应该拥有 UINavigationController,用于管理转换和堆栈,以及其他 ViewController(从 UIViewController 继承,而不是 UINavigationController)用于您要显示的内容。查看一些教程,也许是这个 - makeapppie.com/tag/uinavigationcontroller-in-swift
-
谢谢@jovanjovanovic 我用你的建议来编辑我的文件。我在下面发布了更新作为答案。我不确定是否应该将我的答案标记为正确,或者是否应该让您将您的答案贴在下面以便我可以将您的答案标记为正确?
-
我已将我的评论添加为答案 - 您可以将其标记为正确的。谢谢!