【问题标题】:Hiding keyboard on didHideSearchResultsTableView messes up search view在 didHideSearchResultsTableView 上隐藏键盘会弄乱搜索视图
【发布时间】:2014-10-08 10:00:51
【问题描述】:
  • 在创建了一个在 iOS7 上运行良好的搜索显示控制器之后。
  • 我在iOS8 的搜索视图中无法正确使用hide keyboard
  • 结果在底部被削减,字母部分索引器(右侧)与底部垂直对齐 (帖子末尾的屏幕截图)。

基本设置代码:

@interface MyViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, NSFetchedResultsControllerDelegate, ... >

@implementation MyViewController 
@property (nonatomic, retain) UISearchDisplayController *mySearchDisplayController;

- (void)loadView
{
    [super loadView];
    self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
...
}

-(UITableView*) tableView
{
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,0,0)
                                                  style:self.tableViewStyle];
        _tableView.translatesAutoresizingMaskIntoConstraints=NO;
        _tableView.delegate = self;
        _tableView.dataSource=self;
    }
    return _tableView;
}

-(UISearchBar*) searchBar
{
    if(!_searchBar){
        
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,44,144,0)];
        _searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
        _searchBar.translatesAutoresizingMaskIntoConstraints=NO;
        _searchBar.translucent = NO;
        _searchBar.delegate = self;
        _searchBar.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
        _searchBar.autocorrectionType = UITextAutocorrectionTypeNo;        
    }
    return _searchBar;
}


-(void) initWithTableViewStyle: (int) tableViewStyle
{
    //Set the UITableViewStyle
    self.tableViewStyle = tableViewStyle;
    
    //Be sure the searchBar won't overlap the status bar
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    //Add the subviews to the mainView
    [self.view addSubview:self.searchBar];
    [self.view addSubview:self.tableView];

//Create the views dictionaryy
NSDictionary *viewsDictionary = @{@"searchBar":self.searchBar,
                                  @"tableView": self.tableView};

//Create the constraints using the visual language format
[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat: @"H:|[searchBar]|"
                           options:0
                           metrics:nil
                           views:viewsDictionary]];

[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat: @"H:|[tableView]|"
                           options:0
                           metrics:nil
                           views:viewsDictionary]];

[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"V:|[searchBar(==44)][tableView]|"
                           options:0
                           metrics:nil
                           views:viewsDictionary]];
}

现在到有趣的部分,我认为它在 iOS7 和 iOS8 上的工作方式不同,并导致您在下面的屏幕截图中看到问题:

- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)searchDisplayController: (UISearchDisplayController *)controller
 willShowSearchResultsTableView: (UITableView *)searchTableView {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
}
- (void) keyboardWillHide {
    UITableView *tableView = self.searchDisplayController.searchResultsTableView;
    [tableView setContentInset:UIEdgeInsetsZero];
    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
}

选择器似乎在 iOS7 中完成了这项工作,但这是我在 iOS8 上隐藏键盘时得到的结果:

底部行被剪切(滚动条不再启用向下滚动),右侧部分索引器未正确对齐...如何在 iOS8 中进行此操作?

【问题讨论】:

    标签: ios uitableview ios8 uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    我没有看到为表格视图添加布局约束的代码。也许他们在 -(void) initWithTableViewStyle: (int) tableViewStyle 方法中?确保它们设置正确,因为看起来您使用相同的 tableView 来显示所有数据和搜索结果。

    UISearchController 的真正优点在于它让您有机会提供一个单独的视图控制器来管理结果显示。第一个解耦您的类并帮助清理在单个控制器中使用单个 UITableView 或两个 UITableView 创建的混乱。所以你显然陷入了这种混乱,所以我建议更多地研究 UISearchController 并创建一个单独的类来显示结果。这样您就不必担心 contentInsets 等。 不要像使用 UISearchDisplayController 一样使用 UISearchController。

    【讨论】:

    • Ivan - 抱歉,我没有看到你的答案。我用 initWithTableViewStyle 调用和约束编辑了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多