Tuesday, January 31, 2017

Checking whether a view is a subview of another view

Instead of taking all the subviews of a view into an array and looping through to check whether a view is a subview or not, We can use isDescendant method of UIView class too quickly and simply check that condition.

       open func isDescendant(of view: UIView) -> Bool // returns YES for self.

        let parentView:UIView = UIView.init()
        let childView:UIView = UIView.init()
        if (childView.isDescendant(of: parentView)) {

It works for child of Childs and parent of parents also.

Hope this post is useful. Feel free to comment incase of any queries.