如何更改UINavigationBar标题的位置?

7

我已经成功使用自己的导航栏更改了导航栏的高度,但标题仍然居中。我希望它距离左侧的72个像素位置。

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

我用这个来改变高度,但是我没有找到一种改变所有项目位置的方法。我尝试设置框架但失败了。我也无法改变按钮的位置。

我希望它看起来像这样:

enter image description here


将空的导航栏按钮项添加为右侧栏按钮项,并根据您的要求设置其宽度。它会将您的标题移动到左侧。 - Pradhyuman Chavda
6个回答

30
navBar.setTitleVerticalPositionAdjustment(CGFloat(7), forBarMetrics: UIBarMetrics.Default)

7
此回答仅涉及物品的垂直位置,如何回答问题,该问题是关于调整水平位置的? - Tony Adams
4
@TonyAdams 我猜我当时不知道那个问题的答案,所以我只是随便说了点什么来帮助其他人... - arbel03
2
太棒了!这就是我所需要的! - Janserik
这仅用于在垂直方向上更改标题的位置。要水平更改,没有预定义的选项。因此,您可以遵循此问题的第一个答案作为更好的解决方案。 - Pawan kumar sharma
这在iOS 16.0 + Xcode 14.0中“可行”。但是对于破损的约束条件会抛出错误。"_UINavigationBarTitleControl应该等于titleViewGuide.top"。有什么处理方法吗? - theogood

23
创建一个UIView对象,在其中添加UIButtonUILabel以显示类似视图。将此自定义视图添加到导航控制器的左侧栏按钮项中。
用于可视化自定义视图和相关代码的示例截图:

enter image description here

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

谢谢!这是一个好的解决方案,但我刚刚发现Google并没有使用navigationController,它只是一个普通的视图。我稍后会尝试您的方法~ - David

3

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

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

这是最佳答案,不会像在标题上设置锚点那样引起任何问题。 - brokenrhino

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

另一种解决方法是将标题缩进:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 72

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

0

您可以将自定义视图设置为导航项的左/右侧栏项目。

请参考以下示例:

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

[viewModal.getNavBarTitleView() 返回一个水平堆栈视图,其中包含一个 imageView 和一个 label。]


0

设置左对齐的自定义视图。

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

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

  • 当设置右边按钮时,我们无法轻松控制自定义视图的宽度。我们可能会出现自定义视图与右边按钮重叠的情况。
  • 我们需要添加自定义navTitleView,并在每个viewWillAppear和viewWillDisappear中删除它。这是额外的,不是清晰的代码。

所以呢? 我刚刚将宽度约束添加到了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

我们有:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接