繁体   English   中英

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

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

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

期望的结果:

在此输入图像描述

提示和标题:

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

在此输入图像描述

使用自定义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
}

边栏:

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

我通过在视图中添加最顶层的自定义视图解决了这个问题,然后添加了一个标签,它将作为字幕标签。

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

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

暂无
暂无

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

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