【问题标题】:UITableView header disappears under the UINavigationBar using a standard UITableViewControllerUITableView 标头使用标准 UITableViewController 在 UINavigationBar 下消失
【发布时间】:2014-02-26 09:56:07
【问题描述】:

我在UINavigationController 中有一个UITableViewController。我添加了一个UISearchBar 作为tableView.header

UISearchBar *searchBar = [UISearchBar alloc] initWithFrame: CGRectMake(0,0,self.tableView.frame.size.width,44.0)];
self.tableView.tableHeaderView = searchBar;

问题:滚动时tableHeader在navigationBar下消失。

我已经尝试设置navigationController.navigationBar.translucent = NO,但使用标准 UITableViewController 似乎这个技巧不起作用。

有没有办法使用标准的 UITableViewController 来解决这个问题?我希望它能够像在联系人应用程序中一样工作。我的目标是 iOS7。

【问题讨论】:

    标签: ios7 uiviewcontroller uitableview uisearchbar


    【解决方案1】:

    我放弃了使用UITableViewController 并使用标准UIViewController 解决了上面的UISearchBar 和下面的UITableView。通过设置self.edgesForExtendedLayout = UIRectEdgeNoneUISearchBar 在搜索开始时不会与状态栏重叠:

    -(id) initWithTableViewStyle: (int) tableViewStyle
    {
        self = [super init];
        if (self) {
    
            //Set the UITableViewStyle
            self.tableViewStyle = tableViewStyle;
    
            //Be sure the searchBar won't overlap the status bar
            self.edgesForExtendedLayout = UIRectEdgeNone;
    
            //Add the subviews to the mainView
            [self.view addSubview:self.searchBar];
            [self.view addSubview:self.tableView];
    
            //Autolayout
    
            //Create the views dictionary
            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]];
        }
        return 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,0,0,0)];
            _searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
            _searchBar.translatesAutoresizingMaskIntoConstraints=NO;
            _searchBar.translucent = NO;
            _searchBar.delegate = self;
        }
        return _searchBar;
    }
    

    应该制作 UIViewController:

    UIViewController <NSFetchedResultsControllerDelegate,
                                              UISearchBarDelegate,
                                              UISearchDisplayDelegate,
                                              UITableViewDataSource,
                                              UITableViewDelegate>
    

    【讨论】:

      猜你喜欢
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多