UISearchDisplayController and setDisplaysSearchBarInNavigationBar - ios

I'm struggling with the UISearchDisplayController and hope someone out there can help me out.
So basically I've got a UINavigationBar which has a "Search"-Button in it. When someone taps "Search" the UISearchDisplayController should get active and the UISearchBar should appear inside the UINavigationBar. Next to the SearchBar I've implemented a "Cancel"-Button which deactivates the SearchDisplayController and resets the UINavigationBar back to it's standard with the "Search"-Button.
My first problem is the gap between my SearchBar and the results-table or the gray overlay while the SearchDisplayController is active.
The second problem is that I can't reset my rightBarButtonItem to the default icon (magnifying-glass) after tapping cancel. It's nothing happening even when I try to set [self.navigationItem setRightBarButtonItem:nil]; it just remains with the "Cancel" button.
Here is my implementation: (The UI elements are added through my storyboard)
- (IBAction)btnSearchClicked:(id)sender {
[self.navigationController.navigationBar addSubview:self.searchController.searchBar];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelEditing)];
[self.searchController.searchBar setFrame:CGRectMake(0, 0, 250, 44)];
[self.searchController setDisplaysSearchBarInNavigationBar:YES];
[self.searchController setActive:YES animated:YES];
[self.searchController.searchBar becomeFirstResponder];
}
-(void)cancelEditing
{
[self.navigationItem setRightBarButtonItem:nil];
[self.searchController setActive:NO animated:YES];
[self.searchController.searchBar removeFromSuperview];
}

OK I came up with a solution for both problems.
The gap was occurring because of [self.searchController setDisplaysSearchBarInNavigationBar:YES]; which expected the NavigationBar to be translucent, so I've worked around by just enabling translucency on btnSearchClicked and deactivating it on cancel.
The problem with the button was resolved by deactivating setDisplaysSearchBarInNavigationBar in cancelEditing before accessing the setRightBarButtonItem property. Maybe that the setDisplaysSearchBarInNavigationBar-Option creates an new NavigationBar which doesn't respond to self.navigationItem.

Related

Display search bar view correctly in UISearchDisplayController

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.

Button not show in the navigation bar

I try this code for the navigation bar
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,320, 70)];
and for the button I tried this code
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(SaveButtonClicked:)];
self.navigationItem.rightBarButtonItem=add;
after this code i declare SaveButtonClicked method also.
When I try this code there is no error in this code and compiled successfully but the button is not show in the navigation bar.
Help me with right code and suggestions.
After you have created navigation bar
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,320, 70)];
Then You need to create navigation item. and add navigation item to navigation bar
UINavigationItem *item = [[UINavigationItem alloc]init];
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(SaveButtonClicked:)];
item.rightBarButtonItem=add;
navbar.items = [NSArray arrayWithObjects:item, nil];
[self.view addSubview:navbar];
Hope this Helps!
1) You don't need to create a UINavigationBar. The navigation controller you're in provides that.
2) And if you did think you needed one, that probably means your view isn't in a navigation controller at all. Check what the value of self.navigationItem is. I imagine you'll find it to be nil. That means you need to redo your interface so this view is actually inside a navigation controller. "Embed in Navigation Controller" under "Editor" in Interface Builder may be of assistance there.
you can just use this single line to achieve that:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveButtonClicked:)];
after this add this method
-(void)saveButtonClicked:(UIBarButtonItem *)sender{
}
for more you can refer Add button to navigationbar programmatically

Modify the More button on the Navigation bar of a tab displayed from a UITabBarController's More tab

I have an app with a UITabBarController that has more than five tabs.
When I press the More tab, I go to the moreNavigationController which is a UINavigationController.
As you can see, I have figured out how to style the Title, Tint, Table color, and the Edit button on the More screen, as well as the Configure screen from pressing the Edit button.
What I have not been able to figure out is how to style the back button, titled More, when I select an item in the table.
Each tab has it's own class, GRWTabSettingsViewController for example, which inherits from GRWViewController, which provides common functionality for all tabs, which then inherits from UIViewController.
When on the Settings screen (or any other tab), I am trying to edit the More back button.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[(UIBarButtonItem *)[(UINavigationItem *)[(UINavigationBar *)[(UINavigationController *)[self navigationController] navigationBar] topItem] leftBarButtonItem] setTintColor:[UIColor darkGrayColor]];
[self.navigationController.navigationBar.topItem.leftBarButtonItem setTintColor:[UIColor darkGrayColor]];
}
However, this navigationController is clearly the parent since these changes get applied to the More screen and not the Settings screen.
What am I misunderstanding and how would I modify the buttons displayed on the navigation bar of the screen I am viewing?
=== SOLUTION ===
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// use backBarButtonItem not leftBarButtonItem
//[(UIBarButtonItem *)[(UINavigationItem *)[(UINavigationBar *)[(UINavigationController *)[self navigationController] navigationBar] topItem] leftBarButtonItem] setTintColor:[UIColor darkGrayColor]];
//[self.navigationController.navigationBar.topItem.leftBarButtonItem setTintColor:[UIColor darkGrayColor]];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:self.navigationController.navigationBar.topItem.title
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[backButton setTintColor:[UIColor darkGrayColor]];
self.navigationController.navigationBar.topItem.backBarButtonItem = backButton;
// these do not work
//[self.navigationController.navigationBar.topItem.backBarButtonItem setTintColor:[UIColor darkGrayColor]];
//[backButton setTintColor:[UIColor darkGrayColor]];
}
Did take me a while to figure out that I can not format the button through self, or format the button after the assignment to self.
You should be customizing backBarButtonItem of the previous navigation item, not of the topItem.

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