在导航栏中移动文本位置 [英] Move text position inside a nav bar

查看:38
本文介绍了在导航栏中移动文本位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)
    }

我的视图控制器中有这个导航栏,上面有文本配置文件.我可以改变它所在的位置吗?例如将其约束到左侧和顶部,使其向下移动更多.

I have this nav bar in my view controller and the text profile on it. Can I change the position in which it is located. For example constraint it to the left and top so it moves it down more.

推荐答案

不是给导航栏提供框架,而是给标签提供框架并将其添加到导航栏中,就像

Instead of giving frame to navigationBar give frame to your label and add it in NavBar like

      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
      }

希望能解决您的问题

这篇关于在导航栏中移动文本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆