我想要一个大标题,下面是小文本,而后退按钮不向下移动。
期望的结果:
带有提示和标题:
我把时间设置为标题,和我想要的相反,是为了把名字放在最前面
使用自定义 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
}
边栏:
直到 View 出现约 500 毫秒后才会设置约束。我已经尝试在 viewDidLayoutSubviews
中设置 titleView
但这没有帮助。如果有人可以帮助我解决这个问题,我将不胜感激。我认为这是一个导航栏/自定义 titleView 问题。
最佳答案
我通过在 View 的最顶部添加一个自定义 View 来解决这个问题,然后向其添加一个标签,该标签将用作字幕标签。
通过使这个自定义 View 停靠在导航栏的底部,横跨整个屏幕宽度并使其高度取决于标签字体大小以及标签的顶部/底部边距, View 将紧密放置在导航栏下方。
然后您应该将导航栏和自定义 View 配置为具有相同的背景颜色,然后移除导航栏的阴影图像。
关于ios - 如何将提示位置移动到标题下方或向后移动按钮位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738483/