How can I get the opposite value of a Bool in Swift?

前端 未结 3 928
予麋鹿
予麋鹿 2021-01-18 15:02

My specific case is I am trying to toggle the nav bar hidden and showing.

    let navHidden = !self.navigationController?.navigationBarHidden
    self.naviga         


        
3条回答
  •  情歌与酒
    2021-01-18 15:20

    navHidden is an optional. And you explictely unwrap that optional (which means you get a crash if navHidden is nil). Clearly something is wrong here. I suggest

    if let navController = self.navigationController {
        let navHidden = navController.navigationBarHidden
        navController.setNavigationBarHidden (!navHidden, animated:true)
    }
    

提交回复
热议问题