【问题标题】:iOS 11 - Navigationbar large title custom offsetiOS 11 - Navigationbar 大标题自定义偏移
【发布时间】:2017-10-24 11:31:14
【问题描述】:

是否可以更改大导航栏标题的 x 偏移量?我想将 x 偏移更改为 36pt。

【问题讨论】:

    标签: swift uinavigationcontroller uinavigationbar ios11 uinavigationitem


    【解决方案1】:

    我刚刚在最新版本的iOS 12中发现,如果简单修改UINavigationBarlayoutMargins属性,会影响大标题。

    let navigationBar = navigationController.navigationBar
    navigationBar.layoutMargins.left = 36
    navigationBar.layoutMargins.right = 36
    

    我尝试了这里提到的关于使用自定义 NSMutableParagraphStyle 的解决方案。这确实有效,但是因为它拉伸了构成大标题的 UILabel 视图,所以当您向下滑动时,它在文本略微增长的地方播放的微妙动画变得非常扭曲。

    【讨论】:

    • 很棒的提示。如果您使用搜索栏,这也适用:searchController.searchBar.layoutMargins.left = X
    • 关于 NSMutableParagraphStyle 方法问题的要点。谢谢!
    【解决方案2】:

    您可以通过这种方式添加额外的偏移量:

    if #available(iOS 11.0, *) {
        let navigationBarAppearance = UINavigationBar.appearance()
        let style = NSMutableParagraphStyle()
        style.alignment = .justified
        style.firstLineHeadIndent = 18
        navigationBarAppearance.largeTitleTextAttributes = [NSAttributedStringKey.paragraphStyle: style]
    }
    

    【讨论】:

    • 这对我来说是很长的标题。我加了style.lineBreakMode = NSLineBreakByTruncatingTail;
    【解决方案3】:

    你不能。您需要编写自己的 NavigationController,为此将 UINavigationController 子类化。

    【讨论】:

      【解决方案4】:

      您必须继承 UINavigationBar,然后覆盖 draw 方法,并在内部进行更改。看看我的工作示例,然后根据需要调整偏移量/样式:

          override func draw(_ rect: CGRect) {
          super.draw(rect)
      
          self.backgroundColor = UIColor.white
          let largeView = "_UINavigationBarLargeTitleView"
          let labelcolor = UIColor(red: 36.0/255.0, green: 38.0/255.0, blue: 47.0/255.0, alpha: 1.0)
      
          for view in self.subviews {
              if largeView.contains(String(describing: type(of: view))) {
                  for v in view.subviews {
                      if String(describing: type(of: v)) == "UILabel" {
                          var titleLabel = v as! UILabel
                          var labelRect = titleLabel.frame
                          let labelInsets = UIEdgeInsets(top: 10, left: 13, bottom: 0, right: 0)
                          let attrText = NSMutableAttributedString(string: "Jobs", attributes: [NSAttributedStringKey.font: UIFont(name: "SFProDisplay-Heavy", size: 30)!, NSAttributedStringKey.foregroundColor: labelcolor])
      
                          labelRect.origin.y += 20
                          let newLabel = UILabel(frame: labelRect)
                          newLabel.attributedText = attrText
                          titleLabel.text = ""
                          if labelRect.origin.y > 0 {
                              titleLabel = newLabel
                              titleLabel.drawText(in: UIEdgeInsetsInsetRect(labelRect, labelInsets))
                          }
                      }
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-22
        • 2020-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多