我想在它下面放一个带有小文字的大标题而不用后退按钮向下移动.
期望的结果:
使用自定义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,multiplier: 1.0,constant: 0)) titleView.addConstraint(NSLayoutConstraint( item: titleView,toItem: descLabel,attribute: NSLayoutAttribute.CenterYWithinMargins,relatedBy: NSLayoutRelation.Equal,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问题.