Skip to content

Instantly share code, notes, and snippets.

@nicklockwood
Last active March 14, 2019 13:08
Show Gist options
  • Save nicklockwood/10399979 to your computer and use it in GitHub Desktop.
Save nicklockwood/10399979 to your computer and use it in GitHub Desktop.
How to put a fixed search bar in a UITableViewController
#import "TableViewController.h"
@interface TableViewController ()
@property (nonatomic, strong) UISearchBar *searchBar;
@end
@implementation TableViewController
static const CGFloat topOffset = 64; // use 20 if there's no navigation bar, or zero if there's no status bar either
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, topOffset, 320, 40)];
[self.view addSubview:self.searchBar];
self.tableView.contentInset = UIEdgeInsetsMake(self.searchBar.frame.size.height, 0, 0, 0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(self.searchBar.frame.size.height, 0, 0, 0);
}
#pragma mark - Table view delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = self.searchBar.frame;
frame.origin.y = scrollView.contentOffset.y + topOffset;
self.searchBar.frame = frame;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1000;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = @"Hello World";
return cell;
}
@end
@ZephiroRB
Copy link

Version SWIFT?

@NduatiK
Copy link

NduatiK commented Aug 26, 2017

@zephiro Almost a year late...I know

class SwiftTableVC: UITableViewController {

var searchBar: UISearchBar!
let topOffset: CGFloat = 64 // use 20 if there's no navigation bar, or zero if there's no status bar either

override func viewDidLoad() {
    super.viewDidLoad()
    searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 40)) //use view width to match any screen
    self.tableView.contentInset = UIEdgeInsets(top: self.searchBar.frame.size.height, left: 0, bottom: 0, right: 0)
    self.tableView.scrollIndicatorInsets = UIEdgeInsets(top: self.searchBar.frame.size.height, left: 0, bottom: 0, right: 0)
    view.addSubview(searchBar)
}

// MARK: - Table view data source
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    if cell == nil {
           cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
    }
        cell.textLabel?.text = "hello World"
        return cell
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1000
}

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    var frame = self.searchBar.frame
    frame.origin.y = scrollView.contentOffset.y + topOffset
    self.searchBar.frame = frame
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment