Why is my Search Bar within the friendsviewcontroller not displaying? - ios

I have two ways of searching for friends within my app. One of them is a snapchat similar interface where the added friends are shown and possibility of adding a friend is shown with a + sign. I tried adding a search bar somewhere within the area programmatically but got no results of nothing displaying. The code was implemented on my FriendsViewController that I am using and this is the current code I have:
-(void)initialiseSearchBar
{
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
searchResultsController.tableView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchResultsUpdater = self;
[self.tableView setContentOffset:CGPointMake(0, 0)];
self.edgesForExtendedLayout = UIRectEdgeNone;
self.searchResults = [NSMutableArray array];
}
Any idea what I have done wrong or should fix? Also I am planning on releasing with iOS 7 compatibility and up all the way until 8.1, I have worries that the terms might be different or there is something depreciated that I do not know of.

You've allocated it but you haven't added it to the view itself unless it was made in storyboard (which you clearly state you didn't do) see my answer on another question... you have to add it as a subview somehow:
Adding A Search Bar

Related

UISearchController - Results show under searchbar

i've been converting my searches from the old UISearchDisplayController to the new UISearchController. I've got it all working now but my last issue is with the results tableview that is shown.
The results tableview is displayed underneath the searchbar, and when you scroll down the tableview then goes through the status bar.
Attached are some images showing the issues, Does anyone have any suggestions as to why this is happening. I've included some of the set up code from my ViewDidLoad.
Search Tableview before searching
Search Tableview after searching & scrolling
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=NO;
[self.navigationController setNavigationBarHidden:NO animated:YES];
[super viewDidLoad];
self.extendedLayoutIncludesOpaqueBars = YES;
self.navigationController.navigationBar.translucent = NO;
[self setNeedsStatusBarAppearanceUpdate];
// create a filtered list that will contain products for the search results table.
self.filteredItems = [NSMutableArray arrayWithCapacity:[self.listContent count]];
// Initially display the full list. This variable will toggle between the full and the filtered lists.
self.displayedItems = self.listContent;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.placeholder = #"";
// Add the UISearchBar to the top header of the table view
self.TV.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
UIView *theView = [[UIView alloc] initWithFrame:TV.frame];
[theView setBackgroundColor:[UIColor colorWithRed:0.090 green:0.388 blue:0.643 alpha:1]];
[TV setBackgroundView:theView];
[TV reloadData];
self.navigationItem.title = #"Search";
`
Thanks,
Andrew

UISearchDisplayController lightbox doesn't cover tableview

I have gone through all the documentation I can find, but I just can't figure out how to solve this issue. When I click on the search text box, the lightbox comes up, but does not cover the whole screen (looks like it is off by ~64dp). Here are a couple of screenshots.
I have my search stuff inside of a tableViewController
Before:
After:
Has anyone had experience fixing this issue?
Here is how I am setting everything up:
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.barTintColor = [UIColor whiteColor];
self.searchBar.delegate = self;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.displaysSearchBarInNavigationBar = YES;
Turns out I had a self.edgesForExtendedLayout = UIRectEdgeNone; in the viewDidLoad, and that messed EVERYTHING up...

How to add UISearchBar to UIView?

I want to add a UISearchBar to an UIView instead of an UIViewController, the problem is that the init method of UISearchDisplayController needs an UIViewController as contentsController:.
I can see the UISearchBar correctly displayed in the view but if I click in it, the bar disappears. Here is my code I'm currently using:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
// this was my approach, but this DOES NOT work
UIViewController *results = [[UIViewController alloc] init];
results.view.userInteractionEnabled = NO;
[self insertSubview:results.view aboveSubview:self.menuList];
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:results];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.tableView.tableHeaderView = self.searchBar;
Before:
After clicking into search bar:
In swift,
I use hidesNavigationBarDuringPresentation to solve this problem.
This problem seems like after the search bar is clicked, it will automatically hide the bar. And it will be fine after you click outside the view.
I add this line:
self.searchController?.hidesNavigationBarDuringPresentation = false;
Hope it works

UISearchDisplayController strange animation and offset

I have created a UITableViewController and added a UISearchDisplayController programmatically. There's no storyboard or nib file as I am doing everything programmatically. Everything seems to work perfectly except when I click on the search bar, it animates weirdly and there's some offset left to the bottom of the search bar.
Here's a video of the behaviour.
And here's the code of Table VC:
// Initialize Search Bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
self.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
// Hide the search bar
[self.tableView setContentOffset:CGPointMake(0, 44)];
I also tried [self setEdgesForExtendedLayout:UIRectEdgeNone]; on the UITableViewController's implementation but it created another issue, although it seems to fix the animation by some extent leaving some gap at the top and the same gap at the bottom of search bar.
Edit: One more thing to noteā€”it can be seen in the video that the result table view is properly aligned with the search bar. So is there any problem with the implementation of main table view? They are same but the former is aligned properly so I am guessing there's a problem with the table view. What do you think?
iOS7 makes programmatically adding these elements unnecessarily difficult. After much testing in my own app, here's how I did it inside of my UITableViewController.
// _searchBar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
self.searchBar.delegate = self;
// _searchController
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:[self searchBar] contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsTableView.frame = [self.tableView frame];
// _tableView
self.tableView.tableHeaderView = [self.searchController searchBar];
Don't worry about hiding the searchBar, as the OS will reset everything to its original state when you hit cancel.

UISearchBar animation issue

I have a UIViewController in which I want to show a tableview with the serchBar.
//viewDidLoad
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,
0,
SCREEN_WIDTH(),
SCREEN_HEIGHT())
style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
// adding uisearch bar
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_tableView.tableHeaderView = searchBar;
//
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
The issue happens when I click inside the uisearch bar so that the animation starts and it looks like it has a 20px unwanted offset.
In your Storyboard, select the problematic controller, look at the Attributes tab and try to edit these settings:
Under Top Bars
Under Opaque Bars
I've solved a similar problem by unflagging these settings.
I found what is causing this issue. Seems that the animation gets messed up when you set navigationBar.translucent to NO. If you make your navigationBar translucent, everything should work fine, but this is definitely not an ideal solution. I'm going to try and find a workaround.
UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,
64,
self.view.frame.size.width,
self.view.frame.size.height)
style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
// adding uisearch bar
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_tableView.tableHeaderView = searchBar;
//
UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
and I just embade my controller with UINavigationcontroller and its working quite well..
You can cancel animation by subclassing UISearchDisplayController and adding this:
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
if(self.active == visible) return;
[self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
[super setActive:visible animated:animated];
[self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
if (visible) {
[self.searchBar becomeFirstResponder];
} else {
[self.searchBar resignFirstResponder];
}
}
codyko gave me an idea. It was the because the navigation bar was no translucent. So I set it to translucent on this view controller and rest when I left with the following code:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.navigationBar.translucent = NO;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBar.translucent = YES;
}
Now that left the navigation bar on this controller slightly faded, so I added a UIView the same color as my navbar just behind it to make it look opaque. I know it isn't perfect but it works nicely.
As a reminder for anyone with similar issues. I needed to add this line that fixed things:
self.edgesForExtendedLayout = UIRectEdgeNone;
Why are you creating the searchBar programmatically instead of in the StoryBoard ?
I'm currently using searchBar, added in storyboard and it's work fine ( I have to change the contentOffset )
I have applied your code,, It works fine for me,, Just hide you navigation bar and start the search bar from y = 20, instead of y = 0;

Resources