以编程方式呈现没有标识符的视图控制器会导致黑屏

时间:2017-10-23 15:10:17

标签: ios swift xcode9

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

我在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中创建的viewcontroller类,然后下面的函数是我的createAccountController中的函数。

@objc func moveToIntermediate() {

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

}

这是intermediateViewController代码

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)

}

}

2 个答案:

答案 0 :(得分:1)

在你的约束中:

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

顶部和底部锚点等于self.view.bottomAnchor

答案 1 :(得分:0)

更新:刚看到Axazeano已经找到了答案。

你在约束中犯了一个小错字:

categoryTableView.topAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

更正版本:

categoryTableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

另外:不要忘记将viewController设置为tableView的dataSource。