iphone - Search bar disappears from my root view controller - iphone

I have a search bar in my main screen - root table view controller. If I browse other screens and come back, sometimes, the search bar disappears. Here's my code.
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
searchBar.delegate = self;
searchBar.tintColor = [UIColor blackColor];
[searchBar sizeToFit];
self.tableView.tableHeaderView = searchBar;
searchDisplayController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
Is there anything wrong with my code or is it one of the quirks of SDK 3.0?

I stumbled across this same issue recently and was able to narrow it's occurrence down to only when the search bar is out of sight (i.e. the table view has been scrolled), then navigated away from, then return to and scrolled back into sight. I have been unable to find any information on what the cause is, but I was able to workaround it by placing this:
self.tableView.tableHeaderView = searchBar;
in either the viewWillAppear or viewDidAppear event of my controller class. I'm assuming the code you've posted is from the viewDidLoad method of your controller class?

Related

Search bar is constant when we scroll the table and the index value(A,B,C,D,E etc) is above the search bar

Please tell me how we can implement this.Search bar in top after that there is a tableview.In a table there is secction header and index value. Search bar is constant when we scroll the table and the index value(A,B,C,D,E etc) is above the search bar.
This will give you standard searchBar and solve your problem.
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];
searchBar.delegate = self;
searchBar.placeholder = #"Search";
self.tableView.tableHeaderView = searchBar;
UISearchDisplayController *searchDC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

issue in picker view inside popover

I am making application in Ipad i have taken one pickerviewController in Xib and i show it when user click on particular button now i am trying to put that picker view in popover
this is how i am trying to achieve this tast
pickerView.hidden=FALSE;
i have created outlet of picker and i unhide it here
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 344)];
popoverView.backgroundColor = [UIColor whiteColor];
[popoverView addSubview:pickerView];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);
//create a popover controller
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
CGRect popoverRect;
popoverRect.origin.x =323;
popoverRect.origin.y = 713;
popoverRect.size.height = 215;
popoverRect.size.width = 70;
[popoverController presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
//release the popover content
[popoverView release];
[popoverContent release];
now the Issue Is that my popover is displaying but it is totally black I am struggling on this please tell me what am i doing wrong or correct the code thank you for help
Seems your picker view is hidden somewhere in the pop over because the frame is not properly set yet. So try to set the frame of the picker view to be equal to the bounds of the pop over as a starting point:
pickerView.frame = popoverView.bounds;
But before adding the picker view as a subview in the pop over, you need to remove the picker view properly from the superview.
Now, by default Xcode will generate the IBOutlet as weak property, and this will cause the picker view to be deallocated when it is removed from the superview. So you will need to declare the picker view as a strong property first.
After that, you can remove it from the current superview:
[pickerView removeFromSuperview];
You should keep a reference to the pop over view controller, for example declaring it is a property, and call dismissPopoverAnimated to dismiss the pop over properly.

How to prevent scroll the tableHeaderView of UItable view, To stick at the top

In my split view application it is not possible to add search bar to the rootView of the split view
So i added search bar dynamically at the tableHeaderView of the ui table view as folows
searchBar = [[UISearchBar alloc] init];
searchBar.frame=CGRectMake(0, self.tableView.frame.origin.y, self.tableView.frame.size.width, 44);
[searchBar sizeToFit];
self.tableView.tableHeaderView = searchBar;
When Scroll down: iThe tableHeaderView also scrolls down so search bar also scrolls
When Scroll top: The tableHeaderView also scrolls top so search bar also scrolls
I implemented code as follows to resolve this issue this helps only when scrolls down but when we scrolls the table view to topside it again move with the table view
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect rect = self.tableView.tableHeaderView.frame;
rect.origin.y = MIN(0, self.tableView.contentOffset.y);
self.tableView.tableHeaderView.frame = rect;
}
I need to stick the tableHeaderView/ Search bar at the top of the view always
How to do this
You can add tabBar separate with tableView
mySearchBar = [[UISearchBar alloc] init];
[mySearchBar setHidden:NO];
mySearchBar.placeholder = #"Search item here";
mySearchBar.tintColor = [UIColor darkGrayColor];
mySearchBar.frame = CGRectMake(0, 0, 320, 44);
mySearchBar.delegate = self;
[mySearchBar sizeToFit];
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[self.view addSubview:mySearchBar];
And tableView
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, 320, 436)];
[self.view addSubview:tableView];
If You want to add in xib then
Put your searchBar in separate view and put that view above the table view. That means it stays constant.
I'm sure this has been answered before, but assuming you're using UITableViewController, you can make the view property be anything you want. So one approach would be to set up a container view with your search bar at the top and the table below it and make view be this container. By default, tableView returns view, so another detail you'd need to take care of is overriding the tableView property to return the actual table view (that you've stored in an ivar). The code might look something like this:
#synthesize tableView = _tableView;
- (void)loadView
{
[super loadView];
_tableView = [super tableView];
// Container for both the table view and search bar
UIView *container = [[UIView alloc] initWithFrame:self.tableView.frame];
// Search bar
UIView *searchBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
// Reposition the table view below the search bar
CGRect tableViewFrame = container.bounds;
tableViewFrame.size.height = tableViewFrame.size.height - searchBar.frame.size.height;
tableViewFrame.origin.y = searchBar.frame.size.height + 1;
self.tableView.frame = tableViewFrame;
// Reorganize the view heirarchy
[self.tableView.superview addSubview:container];
[container addSubview:self.tableView];
[container addSubview:searchBar];
self.view = container;
}

UISearchview responding to actions - TableSearch sample iphone

I would like to modify the tableSearch sample code, so that there is no nib for the tableview. As it is already a tableviewcontroller subclass, I can call the tableview like this:
MainViewController *mainViewController = [[MainViewController alloc] initWithStyle:UITableViewStylePlain];//NibName:#"MainView" bundle:nil];
Without the nib, I would like to add the search bar manually. I tried below:
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,10, 320, 41)];
[searchBar setFrame:CGRectMake(0,10, 320, 41)];
searchBar.delegate = self;
searchBar.tintColor = [UIColor redColor];
[searchBar sizeToFit];
self.tableView.tableHeaderView = searchBar;
The searchbar does appear ok. Now I would like the search action of this searchbar to point to the methods that I already have in my code, i.e searchDisplayController delegate methods.
Is there anyway I can point the self.searchDisplayController's Search bar property to my custom searchbar, so they do the same actions? Thanks in advance...
You have to use the initWithSearchBar:contentsViewController method of UISearchDisplayController. This way, your custom search bar will be associated with the controller. Also you have to set the contentsViewController in this method.
Hm.. this is code from one of my VC. Also set UISearchBarDelegate,UISearchDisplayDelegate
_listContainer = [[UITableView alloc] init];
_listContainer.delegate = self;
_listContainer.dataSource = self;
_listContainer.separatorStyle = UITableViewCellSelectionStyleNone;
_data = [[NSArray alloc] init];
And then
_searchBar = [[NL_CustomSearchBar alloc] initWithFrame:CGRectMake(4.0f, 0, 150, 46)];
_searchBar.delegate = self;
_listContainer.tableHeaderView = _searchBar;

toolbar does not get populated

In my loadView methoad of my view controller I have the following code:
// Populate self.view and add some views/UI elements
// load Gender selection Bar
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.tintColor = [UIColor colorWithRed:0 green:0.37 blue:0.5 alpha:1];
self.genderControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(#"Male", nil), NSLocalizedString(#"Female", nil), nil]];
genderControl.segmentedControlStyle = UISegmentedControlStyleBar;
genderControl.frame = CGRectMake(0, 0, 200, 30);
genderControl.tintColor = [UIColor colorWithRed:0 green:0.37 blue:0.5 alpha:1];
[genderControl addTarget:self action:#selector(changeGender:) forControlEvents:UIControlEventValueChanged];
genderControl.selectedSegmentIndex = GENDER_MALE;
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:genderControl];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[[[self navigationController] toolbar] setItems:[NSArray arrayWithObjects:flexSpace, item, flexSpace, nil] animated:YES];
[item release];
However, if I load the view it displays the toolbar, but the Segmented Control does not get displayed. If I move my code to the viewWillAppear: method it works, but once I hide my view and redisplay it again the segmented control is gone anew.
Does anybody know this problem and/or has an idea how to solve it? Looks very strange to me.
Most of your code looks fine to me, and everything should work when called from the viewDidLoad or the loadView method.
However, when you use the included toolbar from the NavigationController, toolbar items are set on the ViewController rather than the toolbar itself. So replace this line:
[[[self navigationController] toolbar] setItems:[NSArray arrayWithObjects:flexSpace, item, flexSpace, nil] animated:YES];
With this:
[self setToolbarItems:[NSArray arrayWithObjects:flexSpace, item, flexSpace, nil] animated:YES];
Documentation here: http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/setToolbarItems:animated:
You will also want to add this line:
[flexSpace release];
Are you sure that this code should be in loadView? Check the UIViewController docs: loadView is used for constructing your view controller's view entirely programmatically - nothing is loaded from a nib if you implement loadView. Should this code be in viewDidLoad instead? I see that you're not even creating and assigning the main UIView in your loadView method -- which you'd normally expect to see, e.g.:
self.view = [[[UIView alloc] init] autorelease];
If you did mean to implement loadView, as you have, you should add the above line for assigning your main view towards the top of the method, rather than later on.
Btw, perhaps don't bother calling [toolbar setItems] with animated=YES; not much point and I've seen the exact value of the animated parameter break things inexplicably in the past!