【问题标题】:How to change the uinavigationbar title's position?如何更改导航栏标题位置?
【发布时间】:2014-12-18 04:05:23
【问题描述】:

我已经设法通过使用自己的导航栏来更改导航栏的高度,但标题仍然居中。我希望它位于左侧 72px 的位置。

override func sizeThatFits(size: CGSize) -> CGSize {
   return CGSizeMake(UIScreen.mainScreen().bounds.width, 56)
}

我用它来改变高度,但我没有找到改变所有项目位置的方法。 我试图设置框架,但我不能。我也不能改变按钮的位置。

我想变成这样

【问题讨论】:

  • 将空导航栏按钮项添加为右栏按钮项,并根据您的要求设置其宽度。它会将您的标题向左移动。

标签: swift ios8 uinavigationbar material-design


【解决方案1】:
navBar.setTitleVerticalPositionAdjustment(CGFloat(7), forBarMetrics: UIBarMetrics.Default)

【讨论】:

  • 此答案仅针对项目的垂直位置。这如何回答有关调整水平位置的问题?
  • @TonyAdams 我想我当时不知道这个问题的答案,所以我只是向空中扔了一些可以帮助他人的东西......
  • 酷!这就是我需要的!!
  • 这仅在您想垂直更改标题位置时使用。要水平更改,没有预定义的选项。所以你可以按照这个问题的第一个答案是一个更好的解决方案。
【解决方案2】:

创建一个UIView 对象,在其中添加UIButtonUILabel 以显示类似的视图。将此自定义视图添加到导航控制器的左栏按钮项中。

显示自定义视图和相关代码的示例屏幕截图:

override func viewDidLoad() {
    super.viewDidLoad()

    var customView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 44.0))
    customView.backgroundColor = UIColor.yellow

    var button = UIButton.init(type: .custom)
    button.setBackgroundImage(UIImage(named: "hamburger"), for: .normal)
    button.frame = CGRect(x: 0.0, y: 5.0, width: 32.0, height: 32.0)
    button.addTarget(self, action: #selector(menuOpen(button:)), for: .touchUpInside)
    customView.addSubview(button)

    var marginX = CGFloat(button.frame.origin.x + button.frame.size.width + 5)
    var label = UILabel(frame: CGRect(x: marginX, y: 0.0, width: 65.0, height: 44.0))
    label.text = "Inbox"
    label.textColor = UIColor.white
    label.textAlignment = NSTextAlignment.right
    label.backgroundColor = UIColor.red
    customView.addSubview(label)

    var leftButton = UIBarButtonItem(customView: customView)
    self.navigationItem.leftBarButtonItem = leftButton
}

func menuOpen(button: UIButton) {
    // Handle menu button event here...
}

【讨论】:

  • 谢谢!这是一个很好的解决方案,但我刚刚发现谷歌没有使用导航控制器,它只是一个普通的视图。我稍后会尝试你的方法~
【解决方案3】:

另一种解决方案是简单地缩进标题:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 72

navigationBar.titleTextAttributes = [
    .foregroundColor: UIColor.purple,
    .paragraphStyle: paragraphStyle
]

【讨论】:

    【解决方案4】:

    您也可以像这样设置导航栏外观titlePositionAdjustment

    let a = UINavigationBarAppearance()
    a.titlePositionAdjustment = .init(
       horizontal: -CGFloat.greatestFiniteMagnitude, 
       vertical: 0
    )
    navigationItem.scrollEdgeAppearance = a
    navigationItem.compactAppearance = a
    navigationItem.standardAppearance = a
    

    【讨论】:

      【解决方案5】:

      设置自定义视图左对齐。

      将自定义视图设置为leftBarButtonItem并不方便,因为我们会遇到返回按钮的问题。

      将自定义视图设置为navigationBar的子视图并不方便,因为:

      • 当设置了右键时,我们无法轻松控制自定义视图的宽度。我们可以得到重叠的自定义视图和右键
      • 我们需要添加自定义 navTitleView 并在每个 viewWillAppear 和 viewWillDissapear 中删除它。这是额外的,不是干净的代码。

      所以? 我刚刚为 customTitleView 添加了宽度约束。

      CustomTitleView 类内部:

        private func layoutViewsConfig() {
          // Width constraint needed to align view to left
          let widthConstraint = NSLayoutConstraint.init(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: UIScreen.main.bounds.width)
          widthConstraint.priority = .init(748)
          let heightConstraint = NSLayoutConstraint.init(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50)
         self.addConstraint(widthConstraint)
         self.addConstraint(heightConstraint)    
        }        
      

      然后,在您的 ViewController 内部:

      var navigationTitleView = NavigationSubtitleView() 
      self.navigationItem.titleView = navigationTitleView
      

      我们有:

      【讨论】:

        【解决方案6】:

        您可以将自定义视图设置为navigationItem的左/右栏项。

        看这个例子:

        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: viewModal.getNavBarTitleView())
        

        [viewModal.getNavBarTitleView() 返回带有 imageView 和标签的水平堆栈视图。]

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-22
          • 1970-01-01
          • 2015-12-24
          • 2016-10-04
          • 2018-04-03
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多