user6934210
user6934210

Reputation:

Presenting view controller without identifier programmatically results in black screen

I have a viewcontroller that allows users to create accounts. Once the account is created, I am trying to instantiate a new viewcontroller called intermediate vc programmatically. I have searched through stack overflow and I can't seem to find any relevant answers. I'm not able to set an identifier for the viewcontroller since it's not part of storyboard.

I created a button in my createAccountController that should result in the transition to the next screen.

here is the relevant code for my 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 is the viewcontroller class I created within my createAccountController, and then function below is the function in my createAccountController.

@objc func moveToIntermediate() {

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

}

Here is the intermediateViewController code

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)

}

}

Upvotes: 0

Views: 229

Answers (2)

Kqtr
Kqtr

Reputation: 5935

Update: just saw that Axazeano had already found the answer.

You made a small typo in a constraint:

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

Corrected version:

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

Also: do not forget to set the viewController as the dataSource of your tableView.

Upvotes: 0

Axazeano
Axazeano

Reputation: 900

In your constraints:

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

top and bottom anchors equal self.view.bottomAnchor

Upvotes: 1

Related Questions