Reputation: 21966
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:
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.
Upvotes: 5
Views: 4633
Reputation: 1
Add UISearchControllerDelegate;
Then
-(void)willDismissSearchController:(UISearchController *)searchController{
_tableView.contentInset = UIEdgeInsetsMake(20, 0, 44, 0);}
Upvotes: 0
Reputation: 71854
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:
EDIT:
You can forcely scroll table with this code:
tableView.scrollToRowAtIndexPath( NSIndexPath(index: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false)
Upvotes: 7
Reputation: 2727
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
}
Upvotes: 0
Reputation: 27608
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?
Upvotes: 1