【发布时间】:2015-11-09 10:05:13
【问题描述】:
我正在尝试在 UINavigationBar 内显示进度条,如下所述:Showing a UIProgressView inside or on top of a UINavigationController's UINavigationBar。
在 Swift 中自定义 UINavigationController 类如下所示:
class NavigationControllerWithProgress: UINavigationController {
var progressView: UIProgressView!
var progressBottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
hidesBarsOnSwipe = true
hidesBarsWhenVerticallyCompact = true
// adding ProgressView to UINavigationController to allow it to be placed inside the UINavigationBar
// see: https://stackoverflow.com/questions/19211999/showing-a-uiprogressview-inside-or-on-top-of-a-uinavigationcontrollers-uinaviga
progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Bar)
view.addSubview(progressView)
progressView.translatesAutoresizingMaskIntoConstraints = false
progressView.setProgress(0.5, animated: false)
progressBottomConstraint = NSLayoutConstraint(item: progressView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.navigationBar, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -0.5)
view.addConstraint(progressBottomConstraint)
var constraint: NSLayoutConstraint
constraint = NSLayoutConstraint(item: progressView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0)
view.addConstraint(constraint)
constraint = NSLayoutConstraint(item: progressView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0)
view.addConstraint(constraint)
}
}
问题:一旦 UINavigationBar 被 OnSwipe/WhenVerticallyCompact 自动隐藏,约束就会停止工作,即 ProgressView 错位在 StatusBar 下。
我尝试在 updateViewConstraints() 和/或 viewWillLayoutSubviews() 中更新它,这是我目前看到的唯一方法。
由于该常量已用于相对于 UINavigationBar 分隔符的位置(请参阅链接的 SO 线程),因此我尝试使用此方法来测试这种情况下的 updateViewContraints():
override func updateViewConstraints() {
super.updateViewConstraints()
view.removeConstraint(progressBottomConstraint)
progressBottomConstraint = NSLayoutConstraint(item: progressView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.navigationBar, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 30)
view.addConstraint(progressBottomConstraint)
}
请注意此测试中设置为 30 的常数。
通过这样做,我可以看到约束不适合 UINavigationBar 的自动隐藏:当隐藏/显示带有旋转的导航栏(垂直紧凑时)或滑动栏时
- 纵向时 UINavigationBar 下方 30 像素,但是
- UINavigationBar 在横向隐藏时位于最顶部
- UINavigationBar 横向显示时不可见(覆盖)
有什么建议吗?
【问题讨论】:
标签: ios xcode swift autolayout