如何在Xcode中更改导航栏的高度后更改标题的位置

时间:2018-03-27 06:11:18

标签: ios iphone swift xcode

我希望在更改导航高度后更改导航栏图像标题的位置,但它不起作用。我是新手,请帮助我,谢谢你们!

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

}

1 个答案:

答案 0 :(得分:0)

你可以用它来改变: -

   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
}

只需调用您要使用的功能