5

I have a UITableViewController inside a navigation controller, with a search bar. This is how I add the search bar in viewDidLoad:

let resultsController = SearchTableViewController()
resultsController.people = people
searchController = UISearchController(searchResultsController: resultsController)
let searchBar = searchController.searchBar
searchBar.placeholder = "Search a person"
searchBar.sizeToFit()
tableView.tableHeaderView = searchBar
searchController.searchResultsUpdater = resultsController

This is the result:

enter image description here

I tried editing the table view in the storyboard to add a constraint to make it further from the top view's margins, but I can't add contraints, probably because the table view is inside a UITableViewController.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • best solution. create new uiview, leave 20 pixels from top and add uisearchbar inside that headerview and assign whole view as tableheaderview, your problem will resolved. – Ruchish Shah Jun 13 '15 at 11:55
  • 1
    I don't find you have embeded your uitableviewcontroller inside a uinavigationcontroller. From storyboard you can do it by just selecting the view controller and going to Editor > Embed In > Navigation Controller – agy Jun 13 '15 at 11:55
  • It is already in a navigation controller, I can clearly see it from the storyboard file: https://onedrive.live.com/redir?resid=768cf89e7cf13325!4438&authkey=!AKP0Wbsec7-Zo-g&v=3&ithint=photo%2cpng – Ramy Al Zuhouri Jun 13 '15 at 16:43

4 Answers4

7

I think you need this code.

In your viewDidLoad method add this code:

self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)

And your tableview will be something like this:

enter image description here

EDIT:

You can forcely scroll table with this code:

tableView.scrollToRowAtIndexPath( NSIndexPath(index: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • This way when I launch the app I still see the table view and the status bar overlapped, but if I scroll it it gets fixed. Watch this video: https://onedrive.live.com/redir?resid=768cf89e7cf13325!4440&authkey=!ANbvmk8nT8NLO5I&ithint=video%2cmov – Ramy Al Zuhouri Jun 13 '15 at 17:00
  • 1
    @RamyAlZuhouri try adding tableView.scrollToRowAtIndexPath( NSIndexPath(index: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false) – Leo Dabus Jun 13 '15 at 19:45
1

I understand you are writing code in swift but this is how you would hide status bar in objectiveC

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        // iOS 7
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    } else {
        // iOS 6
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    }
}

// Add this Method
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

Here's an answer in swift How do I hide the status bar in a Swift iOS app?

Community
  • 1
  • 1
Sam B
  • 27,273
  • 15
  • 84
  • 121
0

Try to put following code in didFinishLaunching in AppDelegate class

in swift -

    var version = ( UIDevice.currentDevice().systemVersion as NSString ).floatValue

   if (version >= 7) {
        application.setStatusBarStyle(  UIStatusBarStyle.LightContent, animated: true);
        window?.clipsToBounds = true;
        window?.frame =  CGRectMake(0,20,self.window!.frame.size.width,self.window!.frame.size.height-20);
    }

in Objective-c -

    if (UIDevice.currentDevice.systemVersion] floatValue] >= 7) {
        [application setStatusBarStyle:UIStatusBarStyleLightContent];
        self.window.clipsToBounds =YES;
        self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
    }

and add following method in your ViewController class-

Objective-c-

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
 } 

swift-

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}
NehaK
  • 2,639
  • 1
  • 15
  • 31
  • It kinda works, but now there's a black gap instead of the status bar: https://onedrive.live.com/redir?resid=768cf89e7cf13325!4439&authkey=!AI6RsI-Nc4DSBwY&v=3&ithint=photo%2cpng – Ramy Al Zuhouri Jun 13 '15 at 16:52
  • Hi @RamyAlZuhouri , Please try edited code. It will definitely work. – NehaK Jun 13 '15 at 17:29
0

Add UISearchControllerDelegate;

Then

-(void)willDismissSearchController:(UISearchController *)searchController{
_tableView.contentInset = UIEdgeInsetsMake(20, 0, 44, 0);}
Fanatic_L
  • 1
  • 1