ios - 如何将提示位置移动到标题下方或向后移动按钮位置

标签 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
}

边栏:

直到 View 出现约 500 毫秒后才会设置约束。我已经尝试在 viewDidLayoutSubviews 中设置 titleView 但这没有帮助。如果有人可以帮助我解决这个问题,我将不胜感激。我认为这是一个导航栏/自定义 titleView 问题。

最佳答案

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

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

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

关于ios - 如何将提示位置移动到标题下方或向后移动按钮位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738483/

相关文章:

ios - 2 个根据内部文本调整大小并同时具有相等宽度的 UIButton?

ios - 钥匙串(keychain)有时会返回空值

ios - 如何在 NotificationServiceExtension 中设置断点?

ios - iOS Swift 上的应用程序崩溃在带有动画的索引路径中插入行

ios - 从库中选择新图像后,图像查看图像未更改

ios - 如何在关机时或用户在 iOS 中输入错误密码时运行代码

swift - 从 TabBarController 呈现一个特定的 ViewController

iOS,包含 nib 的 swift 容器库?

swift - 从 GKLeaderboard 中提取数据

ios - 我的应用在ios 7中显示黑色状态栏,但我想要默认的白色iOS 7样式