Can't set titleView in the center of navigation bar because back button

前端 未结 10 1563
情话喂你
情话喂你 2020-12-08 02:25

I\'m using an image view to display an image in my nav bar. The problem is that I can\'t set it to the center correctly because of the back button. I checked the related que

10条回答
  •  天涯浪人
    2020-12-08 02:54

    In Swift, this is what worked for me however it´s not the best solution (basically, add it up to navigationBar):

        let titleIV = UIImageView(image: UIImage(named:"some"))
            titleIV.contentMode = .scaleAspectFit
            titleIV.translatesAutoresizingMaskIntoConstraints = false
    
          if let navigationController = self.navigationController{
                navigationController.navigationBar.addSubview(titleIV)
                titleIV.centerXAnchor.constraint(equalTo:  
    navigationController.navigationBar.centerXAnchor).isActive = true
                titleIV.centerYAnchor.constraint(equalTo: navigationController.navigationBar.centerYAnchor).isActive = true
          }
          else{
                view.addSubview(titleIV)
                titleIV.topAnchor.constraint(equalTo: view.topAnchor, constant: UIApplication.shared.statusBarFrame.height).isActive = true
                titleIV.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    
          }
    

提交回复
热议问题