Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulator iPhone X Series -keep getting crash unrecognized selector sent to instance with all buttons

I'm on Xcode 10.1 and the simulator is version 10.1 also but it happened with the previous version from last year too. I didn't bother with it because I was building out the basic ui and backend so i skipped it. Now I'm almost ready to launch and now I need to test on the X series.

Whenever I use any of the other regular simulator iPhones from the 5S - 8+ there are no problems, everything is fine, I touch a button and the action happens. I have an iPhone 7+ and the buttons work fine using the actual device.

But whenever I use the simulator and I choose any of the iPhone X series devices when I touch any button in any view controller I always get a crash

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIBackButtonContainerView titleLabel]: unrecognized selector sent to instance 0x7fdd99c759e0'

whatever this is causes the problem in the X series [_UIBackButtonContainerView titleLabel]

I'm running Xcode in Debug Mode:

enter image description here

What could be the problem?

enter image description here

Code-

lazy var loginButton: UIButton = {
    let button = UIButton(type: .system)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Login", for: .normal)
    button.setTitleColor(UIColor.white, for: .normal)
    button.titleLabel?.font = UIFont(name: "ArialRoundedMTBold", size: 19)
    // I also tried commenting out button.titleLabel?.font = UIFont(name: "ArialRoundedMTBold", size: 19)
    button.backgroundColor = UIColor.lightGray
    button.addTarget(self, action: #selector(loginButtonPressed), for: .touchUpInside)
    button.clipsToBounds = true
    button.layer.cornerRadius = 5
}()

@objc func loginButtonPressed() {
    // do something
}

override func viewDidLoad() {
    super.viewDidLoad()

    // there is a username textfield and a password textField above the button

    view.addSubview(loginButton)

    loginButton.topAnchor.constraint(equalTo: passwordTextField.bottomAnchor, constant: 8).isActive = true
    loginButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16).isActive = true
    loginButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16).isActive = true

    if UIScreen.main.bounds.width == 320 {

        loginButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
    } else {

        loginButton.heightAnchor.constraint(equalToConstant: 45).isActive = true
    }
}

Here is a picture of the button with just a print statement in the action:

enter image description here

Here is a picture of the crash:

enter image description here

enter image description here

like image 900
Lance Samaria Avatar asked Dec 14 '22 11:12

Lance Samaria


1 Answers

I was able to narrow the bug down by a comment @matt made in the comments, he said I'm having quite a lot of trouble understanding what any of the code you've shown could possibly have to do with UIBackButtonContainerView. It seems to me we want to be looking at your back button, not this loginButton. When he said that I looked at other parts of my code.

This LoginVC with the problem is the root vc and I don't have any barButtonItem's in it however I was using this line of code below to hide the text in the backBarButton item in the vc that was getting pushed on so that it would only show a back arrow.

This is in the parent vc that is pushing:

// LoginVC with loginButton inside of it
navigationItem.backBarButtonItem = UIBarButtonItem(title: nil, style: .plain, target: nil, action: nil)

This is in the child vc that’s getting pushed on:

// child vc that’s getting pushed on
if view.frame.width == 414 && view.frame.height == 896 || view.frame.width == 375 && view.frame.height == 812  {

    navigationController?.navigationBar.prefersLargeTitles = true
} else {

    navigationController?.navigationBar.prefersLargeTitles = false
}

I have no idea why I can use this with no problem with the iPhone 5,6,7, and 8 series but not the X series. When I commented this out it worked.

To get it to work and to still hide the text in the next vc I had to change the code to:

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

The difference is in the title: parameter.

When it wasn't working I set the parameter to nil.

To get it to work I set it to empty quotes: ""

Very strange problem???

UPDATE upon further inspection the problem has to do with

navigationController?.navigationBar.prefersLargeTitles = true

It seems that you can't use the above code AND set the backBarButton title to nil. I guess Apple wants to make sure some sort of text is there when using large navigationBar titles.

like image 151
Lance Samaria Avatar answered May 24 '23 01:05

Lance Samaria