Removing subviews from UIScrollView - iphone

I need to remove image subviews from a ScrollView and I tried removing from the array of subviews but that is an NSArray which is immutable.
How can a subview be removed from the scrollviews array of subviews?

NSArray *viewsToRemove = [scrollView subviews];
for (UIView *v in viewsToRemove) [v removeFromSuperview];

You can do this,
[[scrollView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];

This will remove all subviews of a UIScrollView but its scroll indicators:
_scrollView.showsHorizontalScrollIndicator =
_scrollView.showsVerticalScrollIndicator = NO;
[_scrollView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
_scrollView.showsHorizontalScrollIndicator =
_scrollView.showsVerticalScrollIndicator = YES;

for (UIView *v in [scrollView subviews]) {
[v removeFromSuperview];
}

Call -removeFromSuperview on the subview.

Swift
for subview in scrollView.subviews {
subview.removeFromSuperview()
}

Related

Deleting controls programmatically

I want to remove a UITextField/UIButton/UILabel added to a view on the click of button. How can this be achieved in iOS?
To remove you can simply use:
[myTextField removeFromSuperview];
try this one...
Blockquote
UILabel *t1;
NSArray *arr1=[YourView subviews];
for (UIView *v in arr1)
{
if([v isKindOfClass:[UILabel class]])
{
t1 = (UILabel *)v;
if (t1.tag==your_tag)
[t1 removeFromSuperview];
}
}
Blockquote
You can use [myTextField removeFromSuperview]; to get rid of a view.
[myTextField removeFromSuperview]
See UIView documentation.
All controls inherited from UIView.
So you can remove all the controls which added on UIView using like this. By clicking the button action write like this
for(UIView *control in [self.view subviews])
{
[control removeFromSuperview];
}

How can i remove all UIViews from rootViewController

I am currently at topViewController while a UIView is added to my rootViewController. Is there any way that I can remove all UIViews before going to rootViewController via popToRootViewContoller?
In Going back action,type following code:
for (UIView *subview in self.view.subviews) {
[subview removeFromSuperview];
}
And then type popToRootViewContoller.
Add this in viewWillAppear: method of YourViewController
for (UIView *view in self.view.subviews) {
[view removeFromSuperview];
}
Hope it helps you
In viewWillDisappear you can do this task, when you are leaving your rootViewController then this method will call , so you can remove your view at this point.
-(void)viewWillDisappear:(BOOL)animated
{
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
}
Try this:
-(void)viewWillAppear:(BOOL)animated
{
NSArray *subviews = [self.view subviews];
for (UIView *subview in subviews) {
[subview removeFromSuperview];
}
}
[yourView removeFromSuperView]
but added subviews willnot be there when you pop a UIViewController, so i don't think you need to do this

After calling removeFromSuperview "interfacebuilded" objects are gone?

I have a normal Interace in interface builder (there's a UILabel and a UIButton). In code, I create a UIScrollView with a (unknown) number of UILabels (depends by the User). These labels are created in - (void)viewWillAppear to make sure the data is up to date. To remove the labels, in viewWillDisappear i'm calling
for(UIView *subview in [scrollView subviews])
{
[subview removeFromSuperview];
}
The problem is, after the view get's called again, the Objects created with interfacebuilder (the UILabel & UIButton) disappear. Do the get removed by calling [subview removeFromSuperview];? If yes, how to add them again? Everything which is created by code is still thereā€¦
you should add tag to your labels that you add dynamically.
for(int i = 0; i < n; i++){
UILabel *label = [UILabel alloc...];
label.tag = 999999;
}
then.
for(UIView *subview in [scrollView subviews])
{
if([subview isKindOfClass:[UILabel class]] && subview.tag == 999999){
[subview removeFromSuperview];
}
}
Don't remove all subviews. Remove only the labels you added in viewWillAppear.
UILabel, UIButton and UITextfields are basically subclasses of UIView.
So if you remove all the UIViews under the scrollview, it will also remove them too.
for (UIView *myView in [scrollView subviews]) {
if([myView isKindOfClass:[UILabel class]]){
[myView removeFromSuperview];
}
if([myView isKindOfClass:[UIButton class]]){
[myView removeFromSuperview];
}
if([myView isKindOfClass:[UITextField class]]){
[myView removeFromSuperview];
}
}

Remove the content in a UIScrollView Programmatically?

How do you remove the content in a UIScrollView Programmatically? As I have to update the content in it frequently.
I don't think that there is a difference between typical UIView:
for (UIView *v in [scrollView subviews])
[v removeFromSuperView];
You can loop through your scroll view's subviews like:
for(UIView *subview in scrollView.subviews)
[subview removeFromSuperview];
or you can use - [NSArray makeObjectsPerformSelector:] method like:
[scrollView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
for(UIView *vw in [scrView subViews])
[vw removeFromSuperView];
You can use this to remove contents in your scrollview
Following code is use to remove object in scroll view
[[myScrollView viewWithTag:myButtonTag] removeFromSuperview];

Removing view from its superview

I have added a few buttons to self.view. When the user clicks on one button, i will load another view (subView). My code where i am loading the subView is shown below
subView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.scrollView.contentSize.width, self.scrollView.contentSize.height)];
subView.tag=1;
// and then i add various other components to it
[self.view addSubview:subView]; // and finally add it to the view
When the user touches this view, i detect it and remove the view (i have already done the detection part).
This is how i am removing the view
UIView *v = [self.subView viewWithTag:1];
v.hidden = YES;
[v endEditing:YES];
[v removeFromSuperview];
The view dissapears, but i can't click on the buttons or anything that was on the self.view. When i hold the mouse cursor on a button for awhile i could click it otherwise i can't. How can i solve this ?
Try removing the subview like this:
for (UIView *subView in self.view.subviews) {
if (subView.tag == (int)yourSubViewTag) {
[subView removeFromSuperview];
}
}
Should this line:
UIView *v = [self.subView viewWithTag:1];
really be:
UIView *v = [self.view viewWithTag:1];
?
I think your problem is that you do
[self.subView viewWithTag:1];
but the view you want to remove is "owned" by self.view so it should be
[self.view viewWithTag:1];
But if you have a reference to subView, why search it again? Why not immediately:
self.subView.hidden = YES;
[self.subView endEditing:YES];
[self.subView removeFromSuperview];
Btw you might want to look into your memory management, I see you alloc your subView but I don't see it being released, when the view is removed again. Maybe you do and you just don't show the code, in that case forget my comment.