Xcode 6 - iOS 8: programmatically check if view is set

I use class sizes in the interface builder, defining slightly different constructs for different sizes.

One of my views is not set for a specific size. This works as expected, but now I would like to programmatically determine if this view is set or not. Regardless of whether it is set or not, it looks like the view is never null, and I do not see any flag set for verification.

What is the right way to do this?

+5
source share
3 answers

This is not a great solution, but I haven't found a better one yet:

docs argues that "the runtime object for the remote view is still created. However, the view and any associated restrictions are not added to the view hierarchy and the view has the supervisor property nil."

So, the test for a valid supervisor works as a solution, but I found that it should arrive quite late - in viewDidAppear . For example, supervisors are still zero viewWillAppear .

+4
source

You can make an extension for UIView and check if the view has a view. If it is set, it will return true, if it does not return false.

 extension UIView { func isInstalled() -> Bool{ return (self.superview != nil) ? true : false } } 
0
source

From Apple Docs: Add or Remove Views for Size Class

The runtime object for the uninstalled view is still being created. However, the view and any associated restrictions are not added to the view hierarchy, and the view has the supervisor property nil. This is different from hiding. The hidden view is in the hierarchy of views, like any associated restrictions.

You can check by evaluating PossiblyUninstalledView.superView != nil . If true, then the class is set correctly.

-1
source

Source: https://habr.com/ru/post/1204891/


All Articles