suji
suji

Reputation: 1470

Subviews are not removing while removing the superview in iPad

I created a UIView.A search bar is added as subview on the view.The search bar is also created through code.

I have given a UIAnimation to the view to get a dropdown like effect to the view.

Upto this point it is working fine.

The problem is when i remove the superview the view is go but the search bar is still there.

Here are some codes:

@interface ATViewController : UIViewController<UISearchBarDelegate>
{
    UIView *dropView;

    UISearchBar *searchBar;
}

- (void)viewDidLoad
{
    dropView = [[UIView alloc]initWithFrame:CGRectMake(70, 70, 150, 100)];
    searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,150,0)];
        [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(IBAction)buttonClicked:(id)sender{
   // self.searchBar.delegate = self;
    [dropView addSubview:searchBar];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.9];

    dropView.backgroundColor = [UIColor blueColor];
    dropView.frame = CGRectMake(70, 70, 150, 550);
    self.searchBar.frame=CGRectMake(0,0, 150, 40);

    [UIView commitAnimations];
    [self.view addSubview:dropView];
}
-(void)doSingleTap:(UITapGestureRecognizer *)sender
{
    switch (sender.numberOfTapsRequired) {
        case 1:
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.5];

            dropView.frame = CGRectMake(70, 70, 150, 0);
            self.searchBar.frame=CGRectMake(0, 0, 150, 0);
            [UIView commitAnimations];
            break;

        default:
            break;
    }

}

Adding the subview

Adding the Subview

After removing the subView

After removing the view

Upvotes: 1

Views: 355

Answers (1)

Rakesh
Rakesh

Reputation: 3399

You are not removing the super view in code. You are just reducing its height to zero. If you want the searchBar to be clipped along with the dropView then set dropView's clipToBounds to YES. i.e;

dropView.clipToBounds = YES:

before you reduce its height to zero.

Noticed that you are reducing the search bars height to zero as well. But I think you are not allowed to edit the height of the UISearchBar.

Upvotes: 1

Related Questions