iOS 7 Custom UINavigationBar TitleView moves when Pushing or Popping new View Controller

后端 未结 7 1062
谎友^
谎友^ 2020-12-18 18:26

I am using a custom title view for a UINavigationBar with the following code:

// Set a label to the nav bar
THLabel *titleLabel = [[THLabel alloc] init];
tit         


        
7条回答
  •  [愿得一人]
    2020-12-18 18:58

    What worked for me was to create a variable in the view controller that holds your desired title view then initialize it in viewDidLoad. Then you can set that view to the self.navigationItem.titleView in viewWillAppear and it should display properly. No need for setting autoResizeMask, or rightBarButtons, etc.

    Example:

    class ViewController {
        var myTitleImage: UIImageView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            myTitleImage = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
            myTitleImage.contentMode = .scaleAspectFit
            myTitleImage.image = #imageLiteral(resourceName: "my_title_image")
            // etc...
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            self.navigationItem.titleView = self.myTitleImage
        }    
    }
    

提交回复
热议问题