Display search bar view correctly in UISearchDisplayController - ios

I use a UISearchDisplayController and its SearchBar like the picture :
As you can see, the view which need to be over the search bar is not correctly placed because of the view before the search bar that need to be here, but I don't know how can I move it.
I have already this code :
#implementation MySearchDisplayController
- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
[super setActive: visible animated: NO];
[self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO];
[self.searchResultsTableView setFrame:CGRectMake(0, 32, 320, 476)];
[self.searchContentsController.view setBackgroundColor:[UIColor whiteColor]];
[self.searchBar setBarTintColor:[UIColor whiteColor]];
}
#end
How can I fix it ?
I specify that if I delete the blue view behind there is no problem so it's the cause of the problem, but I need to put the view at this place.

Related

Hiding search bar of UISearchDisplayController

I have a UISearchBar with some customizations and I create a UISearchDisplayController like this
self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
I want the search bar to appear on navigation bar so I also set
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
Now the search bar shows in the Navigation Bar, but I want to show the search bar of my UISearchDisplayController only when I tap on a Navigation Bar Button Item. I want to have a behaviour like:
Hide the search bar initially
Show search bar when a navigation bar button is clicked
Hide the search bar when I tap "Cancel" button of
the search bar
I tried to hide/unhide it like:
self.searchDisplayController.searchBar.hidden = YES;
but the code doesn't seem working. I've spent a lot time searching the solution to have the behaviour I want and still no luck. Thanks.
Try this out:
CGRect searchFrame = self.searchDisplayController.searchBar.frame;
searchFrame.size.height = 0;
self.searchDisplayController.searchBar.frame = searchFrame;
self.searchDisplayController.searchBar.hidden = YES;
EDIT: I just tried with below code and it worked. See if this helps you!
- (void)viewDidLoad {
[super viewDidLoad];
[self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:YES animated:YES];
[self performSelector:#selector(test) withObject:nil afterDelay:2.0];
}
- (void)test {
[self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:NO animated:YES];
}
Maybe this is a suitable solution?
To complete task 1, in your TVC lifecycle method viewDidLoad, insert a non-animated scroll that places the search bar beneath the Nav bar...
- (void)viewDidLoad {
[super viewDidLoad];
// Scroll off screen the search bar (44 points)
[[self tableView] setContentOffset:CGPointMake(0, 44)];
// other code for method
}
To complete task 2, in your TVC create a custom action method associated with a UIBarButtonItem, that effectively hides the Nav bar, at the same time revealing the search bar...
- (IBAction)hideNavBar:(UIBarButtonItem *)sender {
[[self navigationController] setNavigationBarHidden:YES animated:YES]
}
To complete task 3, in your TVC use the UISearchDisplayController Delegate method to effectively display the Nav bar and at the same time hide the search bar...
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[[self tableView] setContentOffset:CGPointMake(0, 44)];
// You might also like to...
[self setSearchBarText:nil]; // if you are using a property to hold the search bar text
[self setSearchResults:nil]; // if you are using a property to hold the search results
}

Navigation Bar Changes Height

When I push my UIViewController to the screen from my previous controller it animates the change. But when it finishes loading it resizes my navigation bar and the jumpy transition makes it look bad. How can I fix this? All I'm doing is hiding the navigation bar in Controller A in viewWillAppear and showing it in Controller B in viewDidLoad.
Ok solved it. In viewDidLoad of Controller B (the view controller I'm pushing) add the following:
UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar setBackgroundImage:[UIImage new]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[navigationBar setShadowImage:[UIImage new]];
Then in your UIViewController's XIB make a height constraint on the navigation bar and set it to 68 (from testing the actual line seems to fall in between 68 and 69). Smooth as silk.
edit: If anyone has any better ideas please add them. I'll have to modify this solution for screen rotation so its not perfect.
You can do all in your controller A like this:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}

Navigation Bar in UITableViewController overlaps with status bar

in my UITableViewControllers I have added navigation bars but the problem is that the Title of the navigation bar intersects with the status bar. Normally I would do "positionForBar" and return UIBarPositioningTopAttached but that only works with UIViewControllers. Thus, I used the "prefersStatusBarHidden" method an return YES. Obviously, this bears a cost to the user since they can't view the time and battery life while they're on those screens while using the app. So is there a way to keep the title navigation bar and the status bar from not overlapping, kinda like in iOS 6?
Here's what i'm talking about:
it doesn't look very clean and i'm trying to fix it
In storyboard click on your UITableViewController then simply click on edit -> embed -> navigation controller. You don't need to use it to navigate anywhere but it will setup your title bar correctly for you.
Try with the code
self.edgesForExtendedLayout = UIRectEdgeNone;
I had the same issue. this fixed it:
credit to: malcolmhall
Its just pushing the nav down but works
// in the .h
#property (strong) UINavigationBar* navigationBar;
//in the .m
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"Title";
self.navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectZero];
[self.view addSubview:_navigationBar];
[self.navigationBar pushNavigationItem:self.navigationItem animated:NO];
}
-(void)layoutNavigationBar{
self.navigationBar.frame = CGRectMake(0, self.tableView.contentOffset.y, self.tableView.frame.size.width, self.topLayoutGuide.length + 44);
self.tableView.contentInset = UIEdgeInsetsMake(self.navigationBar.frame.size.height, 0, 0, 0);
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//no need to call super
[self layoutNavigationBar];
}
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self layoutNavigationBar];
}

adding UISearchBar to UICollectionviewController embedded into NavigationController IOS6

I would like to add a searchbar to a UICollectionViewController, that's embedded the following way:
UItabbarController > UINavigationbarController > UICollectionViewController > SearchBar (!)
In this view, the search bar would replace the NavigationBar.
Under the same design, if I test the above with a UITableViewController, the searchbar shows up fine (both programmatically and via the Storyboard)
Problem is I can't get to add the search bar over the UICollectionViewController when I use the StoryBoard framework; it just sits in the middle of the view, and I'm clueless as to how to move it to the top. Plus, it always appears below the UICollectionview, so it's not visible.
So, taking the other route, programmatically:
-(void)viewWillAppear:(BOOL)animated{
self.searchBarTop = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.searchBarTop setPlaceholder:#"Enter your command here"];
self.searchDC = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBarTop contentsController:self];
self.searchBarTop.delegate = self;
[[self navigationController] setNavigationBarHidden:NO animated:animated];
[self.navigationController.navigationBar addSubview:self.searchBarTop];
}
With this, the search bar shows up fine. But unfortunately, when I type in some text, it disappears above the view - presumably because the underlying navBar does so - (don't know why...)
I'm not sure exactly why the searchbar is fine with a UITableViewController, and why it is such a pain for a UICollectionViewController.
That said, anyone has a clue as to why the searchbar/navBar disappear, and how I can fix that ?
Any solution is welcome..
thanks !
-A
Add a Header and put the SearchBar in that (that is what I have done in the past). That being said, I have gotten in the habit of hardly ever using either a UITableViewController (unless I am implementing a StaticCell TableView) or a UICollectionViewController. What I would suggest is to implement a standard UIViewController and just add in your UICollectionView. Size the CollectionView down some and put the SearchBar at the top. This allows you to have a SearchBar that is always displayed (which my users generally like better than having to scroll to the top to change, edit a search)
I use the following code to add a UISearchBar to the UICollectionViewController.
Unfortunately I couldn't make UISearchDisplayController working.
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.collectionView.frame), 44)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.delegate = self;
[self.collectionView addSubview:self.searchBar];
[self.collectionView setContentOffset:CGPointMake(0, 44)];
}
- (void) viewWillAppear:(BOOL)animated{
// to show search bar
[self.collectionView setContentOffset:CGPointMake(0, 0)];
// to hide search bar
[self.collectionView setContentOffset:CGPointMake(0, 44)];
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[searchBar setText:#""];
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
}

Focus on the UISearchBar but the keyboard not appear

I've read so many solution on how can i focus a searchbar to make keyboard appear when i open my search view, and all of that are like this
[searchBar becomeFirstResponder];
mine is
[self.searchDisplayController.searchBar becomeFirstResponder];
but I tried both.
Now, I tried this, and I also added a
[self.searchDisplayController setActive:YES];
because I'm using a SearchDisplayController, but so far the best result i can have is to have the cursor on the searchbar, the uitableview with an overlay on it, but still no keyboard.
If I run the simulator I can type on the searchbar with my computer's keyboard, but on an iPhone I can't do anything.
If you want to give a look to my code: http://nopaste.info/39fcda4771.html
the focus should be executed in viewDidLoad method
Thanks again.
I was showing searchbar on textFieldDidBeginEditing:(UITextField *)textField delegate method.
Keyboard was not showing. So for that first resign textField as firstResponder.
i.e.
[textField resignFirstResponder];
Then call method with delay
[self performSelector:#selector(callSearchBar) withObject:NULL afterDelay:0.2];
Where
-(void)callSearchBar{
[self.searchDisplayController setActive: YES animated: YES];
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController.searchBar becomeFirstResponder];
}
It works
Use the UISearchBarDelegate and declare the searchBar in the header file like this ...
#interface mySearchScreen : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> {
UITableView *myTableView;
NSMutableArray *tableData;
UISearchBar *sBar;//search bar
and declare your search bar in loadView
- (void)loadView {
[super loadView];
sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,30)];
sBar.delegate = self;
[self.view addSubview:sBar];
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 31, 300, 400)];
myTableView.delegate = self;
myTableView.dataSource = self;
[self.view addSubview:myTableView];
tableData = [[NSMutableArray alloc]init];
}
and then use becomeFirstResponder on your search bar in viewDidAppear
- (void)viewDidAppear:(BOOL)animated {
//ensure the keyboard comes up from the start
[sBar becomeFirstResponder];
[super viewDidAppear:animated];
}
The sequence matter
[self.searchDisplayController setActive:YES animated:YES];
[self.searchDisplayController.searchBar becomeFirstResponder];
If no luck then check if delegate of the search display controller is linked. Also worth checking the tint colour of the search bar.
On the simulator, make sure that you click on Toggle Software Keyboard to use the keyboard of the simulator as you can see in the image.

Resources