移动导航栏内的文字位置

问题描述 投票:0回答:1
let longTitleLabel = UILabel()
        longTitleLabel.text = "Profile"
        //longTitleLabel.font = ................
       longTitleLabel.font = UIFont(name: "HelveticaNeue-Bold", size: 31)

override func viewDidAppear(_ animated: Bool) {
        let height: CGFloat = 25
        let bounds = self.navigationController!.navigationBar.bounds
        self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
    }

我的视图控制器里有这个导航栏,上面有文字简介。我可以改变它所在的位置吗。比如约束它在左边和顶部,这样就能让它更多的向下移动。

swift xcode
1个回答
1
投票

不把框架给导航条,而是把框架给你的标签,并把它添加到NavBar中,如

      let longTitleLabel = UILabel()
      longTitleLabel.text = "Profile my boy hello jee wao"
      longTitleLabel.textAlignment = .center
      longTitleLabel.backgroundColor = .red
      longTitleLabel.translatesAutoresizingMaskIntoConstraints = false
      if let navigationBar = self.navigationController?.navigationBar {
         navigationBar.addSubview(longTitleLabel)

         longTitleLabel.rightAnchor.constraint(equalTo: navigationBar.rightAnchor, constant: -20).isActive = true
         longTitleLabel.leftAnchor.constraint(equalTo: navigationBar.leftAnchor, constant: 20).isActive = true
         longTitleLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
         longTitleLabel.heightAnchor.constraint(equalToConstant: navigationBar.bounds.maxY).isActive = true
      }

希望能解决你的问题

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.