ios - 以编程方式呈现没有标识符的 View Controller 会导致黑屏

标签 ios swift xcode9

我有一个允许用户创建帐户的 View Controller 。创建帐户后,我将尝试以编程方式实例化一个名为 intermediate vc 的新 View Controller 。我搜索了堆栈溢出,但似乎找不到任何相关答案。我无法为 View Controller 设置标识符,因为它不是 Storyboard的一部分。

我在我的 createAccountController 中创建了一个按钮,它应该会导致过渡到下一个屏幕。

这里是我的createAccountController的相关代码

let testButton: UIButton = UIButton()
override func viewDidLoad() {

view.addSubview(testButton)
    testButton.backgroundColor = UIColor.black
    testButton.setTitle("Hi", for: .normal)
    testButton.translatesAutoresizingMaskIntoConstraints = false

    testButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    testButton.centerYAnchor.constraint(equalTo: usernameName.centerYAnchor, constant: 20).isActive = true
    testButton.addTarget(self, action: #selector(moveToIntermediate), for: .touchUpInside)
}

intermediateViewController 是我在 createAccountController 中创建的 View Controller 类,下面的函数是我的 createAccountController 中的函数。

@objc func moveToIntermediate() {

    let interMediate = intermediateViewController()
    interMediate.modalTransitionStyle = .crossDissolve
    interMediate.view.layer.speed = 0.2
    self.present(interMediate, animated: true, completion: nil)

}

这里是中间的ViewController代码

class intermediateViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

let floatingButton: UIButton = UIButton()
let categoryTableView: UITableView = UITableView()

override func viewDidLoad() {
    super.viewDidLoad()

    setTableView()
    setNavBar()
    setFloatingButton()

    //when view starts
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

    //you can dispose of stuff here
}

func setTableView() {

    categoryTableView.translatesAutoresizingMaskIntoConstraints = false

    self.view.addSubview(categoryTableView)
    categoryTableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    categoryTableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    categoryTableView.topAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0.0).isActive = true
    categoryTableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = categoryTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    return cell
}

func setNavBar() {

    self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(moveToNext))
    self.navigationController?.navigationBar.backgroundColor = UIColor(red:0.22, green:0.46, blue:0.82, alpha:1.0)
    self.navigationController?.navigationBar.topItem?.title = "Categories"
    self.title = "some title"
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
    self.navigationController?.navigationBar.isTranslucent = false


    //self.transitioningDelegate
    //self.translatesAutoresizingMaskIntoConstraints = false

}

@IBAction func moveToNext() {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let moveToTabBar = storyboard.instantiateViewController(withIdentifier: "TabBarViewController")
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = moveToTabBar

}


func setFloatingButton() {

    floatingButton.translatesAutoresizingMaskIntoConstraints = false

    view.addSubview(floatingButton)
    floatingButton.frame.size = CGSize(width: 150, height: 50)
    floatingButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    floatingButton.centerYAnchor.constraint(equalTo: self.view.bottomAnchor, constant: floatingButton.frame.size.height).isActive = true
    floatingButton.layer.cornerRadius = floatingButton.frame.size.height/2
    floatingButton.backgroundColor = UIColor.black
    floatingButton.setTitle("Done", for: .normal)
    floatingButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
    floatingButton.setTitleColor(UIColor.white, for: .normal)

}

最佳答案

在你的约束中:

categoryTableView.topAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0.0).isActive = true
categoryTableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

顶部和底部 anchor 等于 self.view.bottomAnchor

关于ios - 以编程方式呈现没有标识符的 View Controller 会导致黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46892755/

相关文章:

ios - 在运行时更改 LSApplicationQueriesSchemes

ios - 如何从单元格中检索 detailedText 然后在函数中使用它?

ios - 从 Swift 函数中的异步调用返回数据

ios - 出队时如何阻止 `UIWebView` 中的 `UITableViewCell` 重新加载?

ios - Xcode9 iOS11 应用程序版本中缺少 CFBundleIconName

ios - swift : Adding SSL key to URLRequest

ios - 将 x 位置设置为 SKSpriteNode

ios - deleteRowsAtIndexPaths 重叠问题

ipad - slider 值 iOS

swift - Xcode 9 中指示器没有停止。有解决办法吗?