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

时间:2015-06-09 17:04:07

标签: ios swift

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

期望的结果:

enter image description here

使用提示和标题:

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

enter image description here

使用自定义navigationItem.titleView

enter image description here enter image description here

在第二张照片中,我正在为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
}

边栏:

在视图出现后约500ms才设置约束。我已尝试在titleView中设置viewDidLayoutSubviews但这没有帮助。如果有人能帮助我,我将不胜感激。我认为这是一个navigationBar /自定义titleView问题。

1 个答案:

答案 0 :(得分:0)

我通过在视图中添加自定义视图来解决此问题,然后为其添加标签,该标签将用作字幕标签。

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

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

相关问题