0

我创建SearchBarView了 的子类UIView,并将UISearchBar其作为子视图添加到其中。(从)调用后removeFromSuperview,不会消失。我试图从我创建 SearchBarView 的地方调用,但它没有帮助。任何想法为什么?SearchBarViewsearchBarSearchButtonClickedUISearchBarremoveFromSuperviewSearchBarView

#import <UIKit/UIKit.h>
@interface SearchBarView : UIView <UISearchBarDelegate> {
    UISearchBar *querySearchBar;
}
@end

#import "SearchBarView.h"
@implementation SearchBarView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        querySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,frame.size.width,44)];
        querySearchBar.delegate = self;
        [self addSubview:querySearchBar];
    }
    return self;
}

#pragma mark -
#pragma mark UISearchBarDelegate methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self removeFromSuperview];    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{   
    searchBar.text = @""; 
    [searchBar resignFirstResponder];
    [searchBar setShowsCancelButton:NO animated:YES]; 
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{  
    [searchBar setShowsCancelButton:NO animated:YES]; 
    return YES;   
}  

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{  
    [searchBar setShowsCancelButton:YES animated:YES];  
    return YES;    
}  
@end

SearchBarView 创建如下:

SearchBarView *searchBarView = [[SearchBarView alloc] initWithFrame:CGRectMake(0, 0, 480, 300)];
UIView *rootView = [[[[UIApplication sharedApplication] delegate] window] rootViewController].view;
[rootView addSubview:searchBarView];
4

2 回答 2

0

你的问题是

[self addSubview:querySearchBar];

如果您将搜索控制器添加到子视图,那么当您执行这些操作时它不起作用。

[searchController.searchBar removeFromSuperview()]

self.definesPresentationContext = true

searchController.active = false

我建议你做

presentViewController(searchController, animated: true, completion: nil)

并尝试关闭 searchController

[searchController.searchBar removeFromSuperview()]
于 2016-02-19T15:59:12.563 回答
-2

尝试

[searchbar removefromsuperview];
于 2012-05-07T23:52:13.287 回答