简体   繁体   中英

How to change the postion of the title after changing the height of Navigation Bar in Xcode

I want to change the position of the image title of the navigation bar after changing the height of navigation, but it doesn't work. I'm a rookie,please help me, thank you guys!

override func viewDidAppear(_ animated: Bool)
{
    NavigationBarHeight()
}


func NavigationBarHeight() {


    for subview in (self.navigationController?.navigationBar.subviews)! {
        if NSStringFromClass(subview.classForCoder).contains("BarBackground") {
            var subViewFrame: CGRect = subview.frame
            subViewFrame.size.height = 75
            subview.frame = subViewFrame

        }

    }
}

func addNavBarImage() {
    let navController = navigationController
    let image = UIImage(named: "文字logo")
    let imageView = UIImageView(image: image)



    let bannerWidth = navController?.navigationBar.frame.size.width
    let bannerHeight = navController?.navigationBar.frame.size.height

    let bannerX = bannerWidth! / 2 - image!.size.width / 2
    let bannerY = bannerHeight! / 2 - image!.size.height / 2

    imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth!/2, height: bannerHeight! / 2)
    imageView.contentMode = .scaleAspectFit

    navigationItem.titleView = imageView

}

Well you can use this to change :-

   func title_Logo() {
    let imageView = UIImageView(image: #imageLiteral(resourceName: "icon-logo"))
    imageView.frame = CGRect(x: 0, y: 0, width: 170, height: 30)
    imageView.contentMode = .scaleAspectFit

    let titleView = UIView(frame: CGRect(x: 0, y: 0, width: 170, height: 30))

    titleView.addSubview(imageView)
    titleView.backgroundColor = .clear
    self.navigationItem.titleView = titleView
}

and just call the function where you want to use

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM