iosswiftuinavigationcontrolleruibuttontitleview

Making the nav bar title a button (Swift)


I'm trying to make the title of the nav bar its own button. Users in my app can have multiple profiles, and the nav bar title displays the username of their currently selected profile. Pressing this button should bring up a list of available profiles to choose from (handleShowProfiles). For some reason, the title displays but does not function as a button, it's just static text and touching it does nothing.

let changeProfileContainer : UIView = {
    let container = UIView()
    container.frame = CGRect(x: 0, y: 0, width: 200, height: 40)

    let button = UIButton(type: .custom)
    button.setTitle("@username ▼", for: .normal)
    button.setTitleColor(.black, for: .normal)
    button.frame = container.frame
    button.addTarget(self, action: #selector(handleShowProfiles), for: .touchUpInside)

    container.addSubview(button)
    return container
}()

func configureNavBar() {
    self.navigationController?.navigationBar.tintColor = .black
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "send"), style: .plain, target: self, action: #selector(handleSubmitPost))
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "drafts"), style: .plain, target: self, action: #selector(handleDrafts))
    self.navigationItem.titleView = changeProfileContainer
}

Any ideas why the button part won't function? In Apple's documentation, it says you have to put the button inside a view, make the button type custom, and change the button's frame from it's default (0, 0, 0, 0). I'm pretty sure this is where I'm messing up but I dunno.


Solution

  • Linked to self invocations in computed property -- See the last portion of Ahmad F's answer

    Don't know why, but selectors in computed properties don't seem to work.

    I tried adding the container view without computing it and clicking on the button works.

    func configureNavBar() {
        self.navigationController?.navigationBar.tintColor = .black
        let container = UIView()
        container.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
    
        let button = UIButton(type: .custom)
        button.setTitle("@username ▼", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.frame = container.frame
        button.addTarget(self, action: #selector(pressTitle), for: .touchUpInside)
        container.addSubview(button)
        navigationItem.titleView = container
    }