I used UISearchBar display on UINavigationBar by setting titleView of UINavigationBar at ViewDidLoad.
UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar.searchBarStyle = UISearchBarStyleMinimal;
searchBar.tintColor = [UIColor blackColor];
searchBar.delegate = self;
searchBar.showsCancelButton = NO;
searchBar.backgroundImage = [[UIImage alloc] init];
self.navigationItem.titleView = self.searchBar;
It works, but I had a problem. When I push into NavigationController, UISearchBar delay display until view did appear. I think it seem have to animated when set into titleView. What was problem?
Related
I'm using a UISearchController and a UITableView. When a cell in the UITableView is selected, I'm adding a black UIView above the whole screen with some subviews on it. It works just fine when the UISearchController is inactive. But when it's active, the UISearchBar shows above my black view.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = [NSArray array];
self.searchController.searchBar.delegate = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.backgroundImage = [self imageWithColor:[MySingleton shared].barTintColor];
[self.searchController.searchBar sizeToFit];
[self.view addSubview:self.searchController.searchBar];
myView = [[UIView alloc] initWithFrame:CGRectMake(0, -64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
myView.backgroundColor = [UIColor blackColor];
myView.layer.opacity = 0.9;
[self.view addSubview:myView];
I've tried make it inactive before placing the view,
self.searchController.active = NO;
and add myView to a navigation controller's view
[self.navigationController.view addSubview:myView];
and still no luck. Any idea?
There is a method that will allow you to insert a view above another view in the view hierarchy called: - insertSubview:aboveSubview:. Here's how you could use it:
if (self.searchController.searchBar)
[self.view insertSubview:myView aboveSubview:self.searchController.searchBar];
else
[self.view addSubview:myView];
Try this
[self.searchController setActive:NO];
This is a property of SearchController as per Apple documentation, should do the trick
The UISearchBar is not covering the whole UINavigationBar when tapped. This happens on iOS 7/8 on iPad (It works properly on iPhones). TitleBar and BarButtonItems are still visible in the background.
Following images are of a the UISearchBar set as a HeaderView of a TableView. The
TableViewController is inside a DetailView of a SplitViewController.
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[searchBar setDelegate:self];
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self.emptySearchTableView;
self.tableView.tableHeaderView = searchBar;
i'm using UIRefreshControl with UISearchBar when i'm pull to refresh i see a white background at the top of uiviewcontroller
UISearchBar *searchBar =
[[UISearchBar alloc] initWithFrame:
CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.tableView.tableHeaderView = searchBar;
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:#selector(update) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:_refreshControl];
the problem is the white background above
any ideas?
Create a background view for the table and set its color to clearColor, like this
- (void)viewDidLoad {
self.tableView.backgroundView = [UIView new];
self.tableView.backgroundView.backgroundColor = [UIColor clearColor];
}
Scenario
In my app there is a navigation bar with a "magnifying glass" icon in the upper righthand side. When the user taps on this icon the titleView for the navigation bar will be replaced by a UISearchBar.
I added the searchBar with this code...
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar.delegate = self;
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
All of this works perfectly as it should. Now I need to have the UISearchBar disappear when the user ends their editing of the UISearchBar and to be replaced with the titleView's "title" again.
Question
How do I remove the UISearchBar and display the title for the navigation controller after editing?
Try This,
searchBar.hidden = Yes;
I haven't tested it but this should work:
[self.navigationItem.titleView removeFromSuperview];
self.title = #"Nav bar title";
With the following code the scope bar is invisible (buttons work but are not visible - just black space)
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44 + (_showScope?40:0));];
searchBar.barStyle = UIBarStyleBlack;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:#"string1", #"string2", nil];
searchBar.showsScopeBar = YES;
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
works fine in iOS6. Any ideas?
this seems to be a workaround:
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.barStyle = UIBarStyleBlack;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:#"string1", #"string2", nil];
searchBar.showsScopeBar = YES;
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
searchBar.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 44 + (_showScope?40:0));
nice!
Thank you to Nick H247 for getting me on the right track. However, his workaround produced another issue for me where the search result list was being displayed behind the search bar, making it so I was unable to select search results from the beginning of the list. I fixed this by setting the frame before assigning the UISearchBar to the tableHeaderView.
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.barStyle = UIBarStyleBlack;
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:#"string1", #"string2", nil];
searchBar.showsScopeBar = YES;
searchBar.delegate = self;
searchBar.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 44 + (_showScope?40:0));
self.tableView.tableHeaderView = searchBar;
Simple, calls searchBar.sizeToFit;