首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法删除UISearchBar

无法删除UISearchBar
EN

Stack Overflow用户
提问于 2012-05-08 06:59:58
回答 2查看 361关注 0票数 0

我创建了SearchBarView,这是UIView的子类,并将UISearchBar作为子视图添加到其中。在SearchBarView上调用removeFromSuperview (从searchBarSearchButtonClicked)后,UISearchBar不会消失。我试图从我创建SearchBarView的地方调用SearchBarView上的removeFromSuperview,但没有帮助。你知道为什么吗?

代码语言:javascript
复制
#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的创建方式如下:

代码语言:javascript
复制
SearchBarView *searchBarView = [[SearchBarView alloc] initWithFrame:CGRectMake(0, 0, 480, 300)];
UIView *rootView = [[[[UIApplication sharedApplication] delegate] window] rootViewController].view;
[rootView addSubview:searchBarView];
EN

回答 2

Stack Overflow用户

发布于 2016-02-19 23:59:13

你的问题是

代码语言:javascript
复制
[self addSubview:querySearchBar];

如果您将searchcontroller添加到子视图,则当您执行这些操作时,它将不起作用。

代码语言:javascript
复制
[searchController.searchBar removeFromSuperview()]

self.definesPresentationContext = true

searchController.active = false

我建议你做

代码语言:javascript
复制
presentViewController(searchController, animated: true, completion: nil)

然后试着打发searchController

代码语言:javascript
复制
[searchController.searchBar removeFromSuperview()]
票数 0
EN

Stack Overflow用户

发布于 2012-05-08 07:52:13

试一试

代码语言:javascript
复制
[searchbar removefromsuperview];
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10490274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档