Check if a subview is in a view

check if view contains subview ios swift
uiview
check if stackview contains view
check if view is in hierarchy
swiftui get view by tag
check if uiview is in view hierarchy
check if view is visible on screen swift
get subview with tag swift

I'm making an app where I add a subview to a view using addSubview: on an IBAction. In the same way, when the button with that IBAction is touched again should call removeFromSuperview on that subview added on that IBAction:

PSEUDO CODE

-(IBAction)showPopup:(id)sender 
{
    System_monitorAppDelegate *delegate = (System_monitorAppDelegate *)[[UIApplication sharedApplication] delegate];
    UIView *rootView = delegate.window.rootViewController.view;

    if([self popoverView] is not on rootView) 
    { 
        [rootView addSubview:[self popoverView]];
    } 
    else 
    {
        [[self popoverView] removeFromSuperview];
    }

}

You are probably looking for UIView's -(BOOL)isDescendantOfView:(UIView *)view; taken in UIView class reference.

Return Value YES if the receiver is an immediate or distant subview of view or if view is the receiver itself; otherwise NO.

You will end up with a code like :

Objective-C
- (IBAction)showPopup:(id)sender {
    if(![self.myView isDescendantOfView:self.view]) { 
        [self.view addSubview:self.myView];
    } else {
        [self.myView removeFromSuperview];
    }
}
Swift 3
@IBAction func showPopup(sender: AnyObject) {
    if !self.myView.isDescendant(of: self.view) {
        self.view.addSubview(self.myView)
    } else {
        self.myView.removeFromSuperview()
    }
}

Check if a subview is in a view using Swift, You can use the UIView method isDescendantOfView : if mySubview. isDescendantOfView(someParentView) { someParentView.mySubview. The order of the subviews in the array reflects their visible order on the screen, with the view at index 0 being the back-most view. For complex views declared in UIKit and other system frameworks, any subviews of the view are generally considered private and subject to change at any time.

Try this:

-(IBAction)showPopup:(id)sender
{
    if (!myView.superview)
        [self.view addSubview:myView];
    else
        [myView removeFromSuperview];
}

isDescendantOfView:, The view to test against the receiver's view hierarchy. Return Value. YES if the receiver is an immediate or distant subview of view or if view is the receiver itself; � subview definition: Noun (plural subviews) 1. (computing, graphical user interface) A secondary or subsidiary view.Origin sub- +‎ view

    UIView *subview = ...;
    if([self.view.subviews containsObject:subview]) {
        ...
    }

How to find a UIView subview using viewWithTag(), If you need a quick way to get hold of a view inside a complicated view hierarchy, all subviews, and all sub-subviews, and so on, until it finds a view with the matching tag number. Verify receipts without your own server. Some times you may need to create a SubView and add it into the root view object programmatically. This article will just show you an example about how to do it. 1. Add iOS SubView Example. First let us look at the example demo video as below.

The Swift equivalent will look something like this:

if(!myView.isDescendantOfView(self.view)) {
    self.view.addSubview(myView)
} else {
    myView.removeFromSuperview()
}

Determine if UIView is visible to the user?, My View is added as subview several times into a Tab Bar Controller . Each instance of this view has a NSTimer that updates the view. However I don't want to� A Vertical Stack View will return the first subview for the first baseline and the last for the last. If either of these subviews are themselves Stack Views, then their first or last baseline will be used. A Horizontal Stack View will use its tallest subview for both the first and last baseline.

Check the superview of the subview...

-(IBAction)showPopup:(id)sender {
    if([[self myView] superview] == self.view) { 
        [[self myView] removeFromSuperview];           
    } else {
        [self.view addSubview:[self myView]];         
    }
}

Check if a subview is in a view, I'm making an app where I add a subview to a view using addSubview: on an IBAction . In the same way, when the button with that IBAction is� To create a view in your own schema, you must have the CREATE VIEW system privilege. To create a view in another user's schema, you must have the CREATE ANY VIEW system privilege. To create a subview, you must have the UNDER ANY VIEW system privilege or the UNDER object privilege on the superview.

How to remove the subviews from superview programmatically in , Using isDescendantOfView check subview is present or not if present then remove it from super view. Happy coding!!!! 7.6K views �. View� By selecting Update Using View Rules, you can ensure that MDAC generates update and insert operations against the view itself. Check Option Indicates that when you open this view and modify the Results pane, the data source checks whether the added or modified data satisfies the WHERE clause of the view definition.

[WIP] UIView to check is visible on screen. Not perfect solution � GitHub, [WIP] UIView to check is visible on screen. Not perfect let window = window, // In a window's view hierarchy. window. for subview in subviews {. if subview. View creators are very similar to view composers; however, they are executed immediately after the view is instantiated instead of waiting until the view is about to render. To register a view creator, use the creator method: View::creator('profile', 'App\Http\View\Creators\ProfileCreator'); Optimizing Views. By default, views are compiled on

Finding The First Common Superview In Swift, The child view in the relationship is known as the subview and the parent To test this, create a ViewController class and add the following:� Create the SubView. Since my objects are all over the model, I need to drag them from the tree to an existing SubView. So I need to create the SubView first. Easiest way to do that is mouse-right-click on the ‘SubView’ tree parent node and choose ‘New SubView.’ Once it’s created, you’ll see a new model space on the right.

Comments
  • Doesn't works, just adds the view over it. I'll edit the code to show the real case
  • @zad0xsis - I don't understand the real case. If you added the second view using addSubview: method (which is probably the case the first time), the next time, you'll reach the else part because the second view now is a subview of the first. Isn't it what you were trying to do ? You are maybe looking another mecanism like prensenting a view controller modally ?
  • well, popoverViewis a subview of rootView (rootViewController). I want to check if it's on screen (if it has rootView as superview) and if so remove it or else add it if it's not
  • is [self popoverView] returning a newly created popover each time? it cannot add a view "over it" if it's the same view, adding a view that's already there is a no-op. if the popoverView method is creating a new one every time, then it will always not be in the view hierarchy
  • One important consideration that tripped me up here: when removing then adding subviews that you're holding onto with IBOutlet properties (or ivars) you need to make sure the properties (ivars) are strong, or that (pre ARC) they're retain. By default if you ctrl-drag from a view to a controller to create an outlet, it will create it as weak because it assumes that the view created in the nib will own it so you dont need a strong reference. But if you then remove it programmatically, it will dealloc the control and set your reference to nil (in ARC).
  • awesome! sometimes it needs myView.view.superview, but it worked like magic for me. Thanks @MarkGranoff
  • Doesn't works, just adds the view over it. I'll edit the code to show the real case – zad0xsis 1 min ago edit
  • @pmerino Congrats for being that lazy that you also copied "zad0xsis 1 min ago edit" :-)
  • I think this is more expensive than the other solutions to be honest because it has to iterate through all subviews
  • This is a slight modification of Michael Frederick's answer. Does it give different results from Vincent's code which uses isDescendantOfView:?
  • It absolutely yields a different result. This only checks if it is a child view, not a grandchild or great grandchild, etc.