简体   繁体   English

如何在标题下方移动提示位置或向后移动按钮位置

[英]How to move prompt position below title OR move back button position

I'd like to have a large title with small text underneath it without the back button moving down. 我想在它下面放一个带有小文字的大标题而不用后退按钮向下移动。

Desired outcome: 期望的结果:

在此输入图像描述

With prompt and title: 提示和标题:

I set the time as the title, which is the opposite of what I want, in order to put the name at the top 我将时间设置为标题,这与我想要的相反,以便将名称放在顶部

在此输入图像描述

With custom navigationItem.titleView : 使用自定义navigationItem.titleView

在此输入图像描述在此输入图像描述

In the second photo I'm adding height to the navigationBar . 在第二张照片中,我正在向navigationBar添加高度。 This is very close to what I'd like, but I want the back button to stay at the top. 这非常接近我想要的,但我希望后退按钮保持在顶部。 I can just change the constraints of my custom titleView if I can move the back button. 如果我可以移动后退按钮,我可以更改自定义titleView的约束。

Current code: 当前代码:

func createTitleView() {
  let titleView = UIView()

  let titleLabel = UILabel()
  titleLabel.text = "Pete's Diner"
  titleLabel.textColor = UIColor.whiteColor()
  titleLabel.font = UIFont.boldSystemFontOfSize(16.0)
  titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)

  let descLabel = UILabel()
  descLabel.text = "Open 9AM - 9PM"
  descLabel.textColor = UIColor.whiteColor()
  descLabel.font = UIFont.systemFontOfSize(14.0)
  descLabel.setTranslatesAutoresizingMaskIntoConstraints(false)

  titleView.addSubview(titleLabel)
  titleView.addSubview(descLabel)

  titleView.addConstraint(NSLayoutConstraint(
    item: titleView, attribute: .CenterX, relatedBy: .Equal,
    toItem: titleLabel, attribute: .CenterX, multiplier: 1.0, constant: 0))
  titleView.addConstraint(NSLayoutConstraint(
    item: titleView, attribute: .CenterX, relatedBy: .Equal,
    toItem: descLabel, attribute: .CenterX, multiplier: 1.0, constant: 0))
  titleView.addConstraint(NSLayoutConstraint(
    item: titleView, attribute: NSLayoutAttribute.CenterYWithinMargins, relatedBy: NSLayoutRelation.Equal,
    toItem: titleLabel, attribute: NSLayoutAttribute.CenterYWithinMargins, multiplier: 1.0, constant: 12))
  titleView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
    "V:[titleLabel]-2-[descLabel]", options: nil, metrics: nil, views: ["titleLabel": titleLabel, "descLabel": descLabel]))

  navigationItem.titleView = titleView

  let nav = navigationController?.navigationBar
  nav!.barTintColor = UIColor.customGreenColor()
  nav!.tintColor    = UIColor.whiteColor()
  nav!.translucent  = false
  nav!.frame.size.height = 60.0
}

Sidebar: 边栏:

The constraints aren't set until ~500ms after the view appears. 视图出现后约500ms才会设置约束。 I've tried setting the titleView in viewDidLayoutSubviews which doesn't help. 我已经尝试在viewDidLayoutSubviews设置titleView ,但没有帮助。 If anyone can help me with this, I'd be grateful. 如果有人能帮助我,我将不胜感激。 I'm thinking it's a navigationBar/custom titleView issue. 我认为这是一个navigationBar /自定义titleView问题。

I solved this by adding a custom view topmost in the view, then add a label to it, which will serve as the subtitle label. 我通过在视图中添加最顶层的自定义视图解决了这个问题,然后添加了一个标签,它将作为字幕标签。

By making this custom view dock to the bottom of the navigation bar, span the entire width of the screen and have its height depend on the label font size as well as top/bottom margins to the label, the view will be tightly placed below the navigation bar. 通过使此自定义视图停靠在导航栏的底部,跨越屏幕的整个宽度并使其高度取决于标签字体大小以及标签的顶部/底部边距,视图将紧密地放置在导航栏。

You should then configure the navbar and custom view to have the same background color, then remove the shadow image for the navigation bar. 然后,您应该将导航栏和自定义视图配置为具有相同的背景颜色,然后删除导航栏的阴影图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM