0

I made an xib and showed it inside a view by following code.

    NSArray *xibContents = 
        [[NSBundle mainBundle] loadNibNamed:@"PickupAddressView"
            owner:self 
            options:nil];

    UIView *view = [xibContents objectAtIndex:0]; // Safer than objectAtIndex:0
    //UIView *subView=[view.subviews objectAtIndex:0];

    [view setFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
    CGRect tempFrame=view.frame;
    tempFrame.size.width=300;//change acco. how much you want to expand
    tempFrame.size.height=350;
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:0.2];
    view.frame=tempFrame;
    //[UIView commitAnimations];
    [view setFrame:CGRectMake(10.0f, 90.0f, 300.0f, 350.0f)];
    [view setBackgroundColor:
      [UIColor colorWithPatternImage:
        [UIImage imageNamed:@"Register_bg"]]];
    [UIView commitAnimations];

    [view removeFromSuperview];
    [self.view addSubview:view];

I am using UIStoryboard, and made this xib and showed from following code.

How can I remove this view. I have a UIButton, whose IBAction is being called.

Thanks

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

2 Answers2

9

Keep a weak reference to the view (either a property or an instance variable) and call removeFromSuperview like so:

[self.viewToRemove removeFromSuperview];
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
2

It's too simple to remove any view from its super view just type

[yourview removeFromSuperview];

If you declaring view as a property, then use like below.

[self.yourview removeFromSuperview];
Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36
  • I didn't give downvote to you. If you know answer, just look at this question, if your answer is already provided by some one, just give upvote to them or just leave it. Or add some more point and post as answer – Mani May 02 '14 at 13:11