Hide UITableView search bar by default when in a navigation controller

前端 未结 5 977
萌比男神i
萌比男神i 2021-02-03 14:05

I\'ve read multiple posts on this but it\'s not working properly for me. I\'m using the latest 4.2 SDK.

The code I have is

self.tableView.contentOffset =         


        
相关标签:
5条回答
  • 2021-02-03 14:36

    I also have the same problem like yours. The following code solved my problem.Please add the code in you viewDidLoad() :

    self.edgesForExtendedLayout = UIRectEdgeNone;
    

    N:B: I used autoLayout in my project.

    0 讨论(0)
  • 2021-02-03 14:40
    self.tableView.contentOffset = CGPointMake(0.0, 44.0);
    

    The above code does in fact work but it needs to run after the UITableView has finished creating all of its cells. I guess thats another question though.

    0 讨论(0)
  • 2021-02-03 14:42

    You can set the initial bounds of the table view inside viewDidLoad, so the search bar appears hidden at the beginning.

    You have to create the searchBar property and then use following code:

    - (void)viewDidLoad
    {
        //...
        CGRect bounds = self.tableView.bounds;
        bounds.origin.y = self.tableView.bounds.origin.y + searchBar.bounds.size.height;
        self.tableView.bounds = bounds;
        //...
    }
    
    0 讨论(0)
  • 2021-02-03 14:52

    For others still looking for an updated solution, you can check out my answer over here.

    Basically you need to update the contentOffset the first time viewDidLayoutSubviews is called.

    0 讨论(0)
  • 2021-02-03 14:57

    Another approach should be...in viewDidLoad call:

    self.tableView.contentInset = UIEdgeInsetsMake(-self.searchDisplayController.searchBar.frame.size.height, 0, 0, 0);
    

    and implementing endDragging delegate method:

    -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
        CGPoint offset = self.tableView.contentOffset;
    
        CGFloat barHeight = self.searchDisplayController.searchBar.frame.size.height;
        if (offset.y <= barHeight/2.0f) {
            self.tableView.contentInset = UIEdgeInsetsZero;
        } else {
            self.tableView.contentInset = UIEdgeInsetsMake(-barHeight, 0, 0, 0);
        }
    
        self.tableView.contentOffset = offset;
    }
    

    setting content is to remove some "flickering"

    also if You want searchbar to stick at the top, implement didScroll this way:

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
         CGRect sbFrame = self.searchDisplayController.searchBar.frame;
         sbFrame.origin.y = self.tableView.contentOffset.y;
         if (sbFrame.origin.y > 0) {
             sbFrame.origin.y = 0;
         }
         self.searchDisplayController.searchBar.frame = sbFrame;
    }
    

    I hope this will help (took me few days to figure out:) )

    Cheers!

    UPDATE:

    As @carbonr noted You should add this line in viewDidLoad since iOS7+

    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    0 讨论(0)
提交回复
热议问题