2014-02-26 31 views
2

我在UINavigationController中有UITableViewController。我加了UISearchBartableView.headerUITableView標題在UINavigationBar下使用標準UITableViewController消失

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

問題:當滾動的tableHeader的的導航欄下消失。

我已經嘗試設置navigationController.navigationBar.translucent = NO但似乎這個技巧不能使用標準的UITableViewController。

有沒有辦法解決這個問題,使用標準的UITableViewController?我希望它與聯繫人應用中的效果完全一樣。我的目標是iOS7。

回答

1

我放棄了使用UITableViewController,我解決了使用標準UIViewController頂部UISearchBar和它下面的UITableView。通過設置self.edgesForExtendedLayout = UIRectEdgeNone當搜索開始UISearchBar不重疊的狀態欄:

-(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>