Reputation: 6707
I am using class sizes in interface builder, defining slightly different designs for different sizes.
One of my view is not installed for a specific size. That works as expected, but now I would like to programmatically be able to tell if that view is installed or not. Whether it is installed or not, it looks like the view is never nil, and I can't see any isInstalled flag to check.
What is the correct way to do this?
Upvotes: 5
Views: 2298
Reputation: 1889
You could make an extension to UIView and check to see if the view has a superview. If it's Installed it will return true, if it's no it will return false.
extension UIView {
func isInstalled() -> Bool{
return (self.superview != nil) ? true : false
}
}
Upvotes: 0
Reputation: 1341
From Apple Docs:Installing and Uninstalling Views for a Size Class
A runtime object for an uninstalled view is still created. However, the view and any related constraints are not added to the view hierarchy and the view has a superview property of nil. This is different from being hidden. A hidden view is in the view hierarchy along as are any related constraints.
You could check by evaluating PossiblyUninstalledView.superView != nil
. If it is true, then the class is properly installed.
Upvotes: 0
Reputation: 6754
This isn't a great solution, but I've not found a better one yet:
The docs state that "A runtime object for an uninstalled view is still created. However, the view and any related constraints are not added to the view hierarchy and the view has a superview property of nil".
So a test for a valid superview works as a solution, but I've found that it has to come quite late - in viewDidAppear
. The superviews are still nil in viewWillAppear
, for example.
Upvotes: 5