Reputation: 321
I am following ios11-app-development-bootcamp by Angela. I am having problem to update width of a UIView. Here is the code I am using
@IBOutlet var progressBar: UIView!
progressBar.frame.size.width = (view.frame.size.width / 13) * CGFloat(questionNumber + 1)
The width is setting correctly but the UI is not updating. I have searched and found below solution.
@IBOutlet weak var progressBarWidthConstraint: NSLayoutConstraint!
progressBarWidthConstraint.constant = (view.frame.size.width / 13) * CGFloat(questionNumber + 1)
Why is below code working and above code not working. I am watching this lecture link
Thanks in advance.
Upvotes: 1
Views: 625
Reputation: 2315
If you want to redraw your view, you just need to call layoutIfNeeded
Use this method to force the view to update its layout immediately. When using Auto Layout, the layout engine updates the position of views as needed to satisfy changes in constraints. Using the view that receives the message as the root view, this method lays out the view subtree starting at the root. If no layout updates are pending, this method exits without modifying the layout or calling any layout-related callbacks.
Upvotes: 0
Reputation: 11
In viewDidload
it will take an initial width of the view given in the storyboard.
we can set the width in viewDidAppear
or
Add view.setNeedsLayout() Or view.layoutIfNeeded()
in viewDidLoad
after setting the width.
Upvotes: 1