1

I have a UIProgressView that is programmatically added to a storyboard view along with constraints that place it along the bottom of the navigation bar. There is a menu that when opened temporarily hides the navigation bar and when closed unhides it again. After opening and closing this menu the constraints on the UIProgressView place it at the very top of the window, so it is then hidden by the navigation bar. So the layout and constraints seem to be getting recalculated and applied while the navigation bar is hidden. How do I force the constraints to be applied after the navigation bar is unhidden? I've tried layoutIfNeeded to no avail. Also, its a little difficult to know the view hierarchy as the view is a storyboard placed view embedded in a navigation controller, but the custom view controller for this view loads its own view from a xib. Any ideas on how to force the constraints to be applied and decipher the view hierarchy?

bhartsb
  • 1,316
  • 14
  • 39

1 Answers1

1

Without seeing your code is difficult to answer. By the way this works:

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])
    }
}
J.Williams
  • 1,417
  • 1
  • 20
  • 22
  • Looks like the answer is in swift whereas I'm using obj-c. However, this is old so I went ahead and accepted. – bhartsb Nov 09 '15 at 17:08