I am adding a subview (childView) on main view controller (parentView) for selection of some options from the menu. But when I remove it from the view, the properties of parent view returns null. Can anybody explain this behavior. Also I am using ARC.
This is how I am adding subview:
resolutionPopUp=[ResolutionPopUp alloc];
resolutionPopUp.resPopStr = combinedUrl;
[self.view addSubview:resolutionPopUp.view];
And when I remove the subview using:
[self.view removeFromSuperview];
all the properties of the existing view controller returns null.
[self.view removeFromSuperview] removes the parent view from its parent (which will be a UIWindow) and results in self.view being garbaged collected. The correct way to remove the subview, resolutionPopUp.view is this:
[resolutionPopUp.view removeFromSuperview];
This is will remove resolutionPopUp.view from self.view.
self.view is your parentViewController.and you are removing that so this is main reason why you are getting null Value.
instead of that,user
resolutionPopUp=[ResolutionPopUp alloc];
resolutionPopUp.resPopStr = combinedUrl;
[self.view addSubview:resolutionPopUp.view];
[resolutionPopUp.view removeFromSuperView];
this is correct way.
Related
In my first ViewController having property
#property (nonatomic, strong) UIView* snapShotView;
And I call
self.snapShotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
[self.view addSubview: self.snapShotView];
Then I present two viewcontrollers on top of it and dismiss them again. When I am back on the first viewController the snapShotView property is empty, but the view still displays. I want to remove the snapShotView, but I no longer have a reference.
If I only display one ViewController on top of the first ViewController and dismiss it, the property still references the correct view. If I present and dismiss another view on top of it, the property is null once the additional two ViewControllers are dismissed.
How can I reference and remove the snapshoView?
It is really strange that you are loosing the reference to subView, but anyway if you are loosing the reference to subView and because of that if you are not able to remove the subView you added and if that's your problem, I'll say you don't need the reference to remove the subView from view :)
You can do something like this :)
[[self.view subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
This statement will remove all the subViews from your view without reference :)
and in case you have multiple views added to your view and want to remove just a specific view make use of tags :)
self.snapShotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
self.snapShotView.tag = someValue;
[self.view addSubview: self.snapShotView];
and while removing
for (UIView *view in [self.view subviews]) {
if (view.tag == someValue) {
[view removeFromSuperview];
}
}
In my program needs to create view, then create subView and add it in view.
I create view and subView in storyboard. Then in code:
[subView removeFromSuperview];
[view addSubview:subView];
[self.view addSubview:view];
How can I add subView to view in storyboard without code?
You don't actually need to write any code for this. There are many ways to do this:
Select the subview and drag over the view entry in the Objects Explorer of storyboard. Once you leave the mouse hold, the parent view will have a triangle indicating your subview has become its child and the subview will have a bigger indent than your view
Or you can use the "Embed in View" menu as shown in the below pic [source:http://codesheriff.blogspot.co.il/2014/03/8-tips-for-working-effectively-with.html]
open file inspector ->search for view drag and Drop the view in storyboard
so in this you already holding a view now you adding an view its almost like adding a sub view to the main view this is for adding view without code
Open the storyboard and just create the new view inside the subview.
What your doing above is doing nothing.
[subView removeFromSuperview]; //Your removing subview from the superview
[view addSubview:subView]; //is view a new UIView?
[self.view addSubview:view];
You Will have a View in the bottom of right side.You Just drag and Drop the view Where you Want.To Set constriants clearly We are goin Dynamic view Creation.If you do View creation programmatically it will help you in the future project.
I want to add a view called anotherView on top of self.view. But I don't want anotherView to be a subview. Is this possible?
I want anotherView to be at the top of the page and push the contents of self.view down accordingly (without having to change the y or height values.
I have the following and it doesn't work:
UIView *anotherView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 80)];
anotherView.backgroundColor = [UIColor yellowColor];
[self.view insertSubview:anotherView aboveSubview:self.view];
The box is simply on top of the existing view.
Any visible view is subview of some other view or window.
But you can add anotherView to
self.view.superview
or even
self.view.window
or create new UIWindow object and add your subview to this window
Have you tried to present anotherView modally? That way you can make it to appear on top of everything being a completly new view controller. Ex:
[self presentModalViewController:anotherView animated:YES];
assuming of course anotherView is a subclass of UIViewController and has its own View property. Because if anotherView is just subclass of UIView then you can't present it without being a subview of the current View Controller.
EDIT:
Oh yes, you can just pass a nil to the completion block:
[self presentViewController:anotherView animated:YES completion:nil];
In my app I am trying bring a subview to front, then put it back to its original layer position later. The code should be pretty simple:
To bring the subview to front (inside my custom UIView class):
[self.superview bringSubviewToFront:self];
Easy. I store the original z position in an instance variable called, you guessed it, zPosition. So, the line before -bringSubviewToFront: is:
zPosition = [self.superview.subviews indexOfObject:self];
So, all of the code I use to bring my subview to front is:
zPosition = [self.superview.subviews indexOfObject:self];
[self.superview bringSubviewToFront:self];
This works as it should. The problem is when I try to put the subview back where it was. I'm simply doing this:
[self.superview exchangeSubviewAtIndex:zPosition withSubviewAtIndex:
[self.superview.subviews indexOfObject:self]];
Using this code, if I have two subviews, this is what happens:
Let's say I have view A and view B. View A is above view B. I tap view B, it comes to the front. I tap view B again (it should go back to where it was), and nothing happens, so it's now on view A. If I now tap view A, it comes to the front, but when I tap it again (so it should go back to its original z position: below view B), all of its sibling views disappear!
Does anyone see what could be causing this problem?
There is no need to remove from superview:
[self.superview insertSubview:self atIndex:zPosition];
exchangeSubviewAtIndex may well put the view back in the right place, but it will also swap another view on top, which wont be what you started with. You might need to do something like this instead of exchangeSubviewAtIndex :
[self retain];
UIView *superview = self.superview;
[self removeFromSuperview];
[superview insertSubview:self atIndex:zPosition];
[self release];
[ Swift solution ]
As other guys said, there is no need to remove and re-add your subviews.
Instead I've found that the most convenient method is:
superView.insertSubview(subviewYouWantToReorder, aboveSubview: subviewWhichShouldBeBelow)
This question and answers were very helpful to me.
I had the requirement to place an overlay between the viewstack which views are above and below the overlay, and i wanted to keep it dynamic.
That is, a view can tell it is hidden or not.
I used the following algorithm to reorder the views.
Thanks to AW101 below for the "No need to remove view".
Here is my algorithm:
- (void) insertOverlay {
// Remember above- and belowcounter
int belowpos = 0, abovepos = 0;
// Controller mainview
UIView *mainview = [self currentMainView];
// Iterate all direct mainview subviews
for (UIView* view in mainview.subviews) {
if ([self isAboveOverlay:view]) {
// Re-insert as aboveview
[mainview insertSubview:view atIndex:belowpos + (abovepos++)];
}
else {
// Re-insert as belowview
[mainview insertSubview:view atIndex:belowpos++];
}
}
// Put overlay in between above and below.
[mainview insertSubview:_overlay atIndex:belowpos];
}
I have a custom UIView that I need to add as a subview in a UIViewController.
But if I use [self.view addSubview:newView]; the app goes in a infinity loop and doesn't start. But if I use self.view = newView then it works. But I need it as a subview.
The UIView contains a grid layout of custom button.
I guess you do that in the loadView method. You must not call the view method of the view controller in loadView because that will itself call loadView! However calling setView: (e.g. self.view = newView) is okay.
My suggestion is to add the subview in viewDidLoad.