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

djv*_*djv 7 ios swift

我想在它下面放一个带有小文字的大标题而不用后退按钮向下移动.

期望的结果:

在此输入图像描述

提示和标题:

我将时间设置为标题,这与我想要的相反,以便将名称放在顶部

在此输入图像描述

使用自定义navigationItem.titleView:

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

在第二张照片中,我正在增加高度navigationBar.这非常接近我想要的,但我希望后退按钮保持在顶部.如果我可以移动后退按钮,我可以更改自定义titleView的约束.

当前代码:

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
}
Run Code Online (Sandbox Code Playgroud)

边栏:

视图出现后约500ms才会设置约束.我试过设置titleViewviewDidLayoutSubviews这于事无补.如果有人能帮助我,我将不胜感激.我认为这是一个navigationBar /自定义titleView问题.

Dan*_*idi -1

我通过在视图的最顶部添加一个自定义视图,然后向其添加一个标签来解决这个问题,该标签将用作字幕标签。

通过将此自定义视图停靠在导航栏的底部,跨越屏幕的整个宽度,并使其高度取决于标签字体大小以及标签的顶部/底部边距,视图将紧紧地放置在导航栏。

然后,您应该将导航栏和自定义视图配置为具有相同的背景颜色,然后删除导航栏的阴影图像。