UIProgressView 约束问题
UIProgressView constraints issue
我有一个以编程方式添加到情节提要视图的 UIProgressView 以及将其放置在导航栏底部的约束。有一个菜单,打开时会暂时隐藏导航栏,关闭时会再次取消隐藏。打开和关闭此菜单后,UIProgressView 上的约束将其置于 window 的最顶部,因此它会被导航栏隐藏。因此,在隐藏导航栏时,布局和约束似乎正在重新计算和应用。如何在取消隐藏导航栏后强制应用约束?我试过 layoutIfNeeded 无济于事。此外,了解视图层次结构有点困难,因为视图是嵌入在导航控制器中的故事板放置视图,但此视图的自定义视图控制器从 xib 加载自己的视图。关于如何强制应用约束和破译视图层次结构的任何想法?
没有看到你的代码很难回答。顺便说一句,这个有效:
override func viewDidLoad() {
super.viewDidLoad()
// if VC is pushed in a navigation controller I add a progress bar
if let navigationVC = self.navigationController {
// create progress bar with .bar style and add it as subview
let progressBar = UIProgressView(progressViewStyle: .Bar)
navigationVC.navigationBar.addSubview(progressBar)
// create constraints
// NOTE: bottom constraint has 1 as constant value instead of 0; this way the progress bar will look like the one in Safari
let bottomConstraint = NSLayoutConstraint(item: navigationVC.navigationBar, attribute: .Bottom, relatedBy: .Equal, toItem: progressBar, attribute: .Bottom, multiplier: 1, constant: 1)
let leftConstraint = NSLayoutConstraint(item: navigationVC.navigationBar, attribute: .Leading, relatedBy: .Equal, toItem: progressBar, attribute: .Leading, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item: navigationVC.navigationBar, attribute: .Trailing, relatedBy: .Equal, toItem: progressBar, attribute: .Trailing, multiplier: 1, constant: 0)
// add constraints
progressBar.translatesAutoresizingMaskIntoConstraints = false
navigationVC.view.addConstraints([bottomConstraint, leftConstraint, rightConstraint])
}
}
我有一个以编程方式添加到情节提要视图的 UIProgressView 以及将其放置在导航栏底部的约束。有一个菜单,打开时会暂时隐藏导航栏,关闭时会再次取消隐藏。打开和关闭此菜单后,UIProgressView 上的约束将其置于 window 的最顶部,因此它会被导航栏隐藏。因此,在隐藏导航栏时,布局和约束似乎正在重新计算和应用。如何在取消隐藏导航栏后强制应用约束?我试过 layoutIfNeeded 无济于事。此外,了解视图层次结构有点困难,因为视图是嵌入在导航控制器中的故事板放置视图,但此视图的自定义视图控制器从 xib 加载自己的视图。关于如何强制应用约束和破译视图层次结构的任何想法?
没有看到你的代码很难回答。顺便说一句,这个有效:
override func viewDidLoad() {
super.viewDidLoad()
// if VC is pushed in a navigation controller I add a progress bar
if let navigationVC = self.navigationController {
// create progress bar with .bar style and add it as subview
let progressBar = UIProgressView(progressViewStyle: .Bar)
navigationVC.navigationBar.addSubview(progressBar)
// create constraints
// NOTE: bottom constraint has 1 as constant value instead of 0; this way the progress bar will look like the one in Safari
let bottomConstraint = NSLayoutConstraint(item: navigationVC.navigationBar, attribute: .Bottom, relatedBy: .Equal, toItem: progressBar, attribute: .Bottom, multiplier: 1, constant: 1)
let leftConstraint = NSLayoutConstraint(item: navigationVC.navigationBar, attribute: .Leading, relatedBy: .Equal, toItem: progressBar, attribute: .Leading, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item: navigationVC.navigationBar, attribute: .Trailing, relatedBy: .Equal, toItem: progressBar, attribute: .Trailing, multiplier: 1, constant: 0)
// add constraints
progressBar.translatesAutoresizingMaskIntoConstraints = false
navigationVC.view.addConstraints([bottomConstraint, leftConstraint, rightConstraint])
}
}