Transforming UISearchbar - ios

I have a search-bar with tableView below that search bar. I want to go full screen when the user tap search-bar for searching. But i am unable to set the frame to the origin of the UISearchBar.
i doing it in this way.
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
[ [UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[self.topBarView setHidden:YES];
[UIView animateWithDuration:1.0
delay:0.0
options:0
animations:^{
[self.searchBar setFrame:CGRectMake(0.0,0.0, self.searchBar.frame.size.width,
self.searchBar.frame.size.height )];
}
completion:^(BOOL completion){
}];
[self.searchBar setNeedsDisplay];
}
}
I am hiding all the views above the search-bar and i am setting the origin to 0,0
In the second image the top bar disappears but search-bar stays on its place. It should go at the top.
If somebody can identify the problem it will be great.
I am also using auto-layout does this make cause any problem?

use searchbar display controller.it will achieve that

Try this.
Add a search Bar & searchDisplay Controller to your controller.
UISearchBar *searchBar;
UITableView *tableView;
UISearchDisplayController *searchController;
// Create table view
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:_tableView];
// Create search bar
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 44.0f)];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
[self.view addSubview:_searchBar];
[_searchBar sizeToFit];
// Create search controller
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchController.searchResultsDataSource = self;
_searchController.searchResultsDelegate = self;
_searchController.delegate = self;
Then you can perform this operation so that searchBar becomes responder & shows Keyboard.
[_searchBar becomeFirstResponder];
Hope that helps.

Related

How to dismiss a UISearchBar programmatically?

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

How to add a custom UIView to Navigation Bar in iOS7+ like this

Is there a way to insert any UIView in NavigationBar like the image below?
Is this not working?
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: #"Controls"];
[navBar pushNavigationItem:navItem animated:NO];
In the code below, I embed a view into the navbar, where my embedded view draws the background and the new buttons. The code also deals with relaying out the view when the device is rotated.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (view.superview == nil) {
[self.navigationBar addSubview:view];
[self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
}
CGRect frame = self.navigationBar.frame;
frame.size.height = MY_NAVBAR_HEIGHT;
self.navigationBar.frame = frame;
view.frame = self.navigationBar.bounds;
}
An alternative solution is to use a regular UIView as a NavBar, and by saying that what I mean is you have to set the UIView in navbar place and hide the NavigationController bar in the main VC and show it in the next one read this article Here https://medium.com/me/stats/post/54bc5fc56d6c

stange behaviour UIRefreshControl with UISearchBar

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];
}

iOS7: Not able to add search bar to Navigation Controller

In my SearchViewController.m, I added
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
[self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];
self.searchResultsView.dataSource = self;
self.searchResultsView.delegate = self;
NSLog(#"fetching businesses");
[self fetchBusinesses];
// fix UITableViewCell height
self.tableView.rowHeight = 90;
}
But I can not see search bar. All I see is
What am I missing?
Thanks
You can add search bar programatically like follows:
make property of both UISearchBar and UISearchDisplayController i.e.
#property (nonatomic,strong) UISearchBar *searchBar ;
#property (nonatomic,strong) UISearchDisplayController *searchDisplayController;
then write following code:
- (void)viewDidLoad
{
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate =self;
self.searchBar.showsCancelButton =TRUE;
[self.searchBar setHidden:NO];
[self.searchBar becomeFirstResponder];
[self.searchDisplayController setActive:YES];
[self.view addSubview:self.searchBar];
self.searchDisplayController =[[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.delegate =self;
self.searchDisplayController.searchResultsDataSource= self;
self.searchDisplayController.searchResultsDelegate = self;
}
also you need to implements UISearchDisplayDelegate, UISearchBarDelegate delegates for the same ViewController
Hope This code will work for you . Give it a try and let me know your feedback.
Now i find the answer , i just tested it on my side and its working fine at my end ,
// init search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
searchBar.showsCancelButton=YES;
// set up searchDisplayController
UISearchDisplayController *searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
[self.searchDisplayController.searchBar setBarTintColor:[UIColor whiteColor]];

Adding UISearchBar to UITableView

Background: I have a UIViewController which has a UITableView added programmatically. At the top of the UITableView I have a tablHeaderView in which I have placed a UIView. This UIView has a UISearchBar and a segmentedControl. The idea being: that a user can sort the UITableView by some basic categories such as 'Date/Time' or 'Location' etc. They can also search by an item in the programme.
Problem: When I tap the search bar it resizes (which I don't want) and then when I cancel the search it resizes again and stays there until the UIViewController is exited and loaded again.
Code:
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
_searchBar.showsCancelButton = YES;
NSLog(#"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Date/Time", #"Location", #"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
[segmentedControl addTarget:self action:#selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:_searchBar];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
self.tableView = _tableView;
}
What I have tried: I have tried setting the size of the SearchBar, not setting its size. Not having it in a UIView in the tableHeaderView, instead just having it there on its own.
Why does it resize at all and how can I get it to stop?
EDIT: I have just tried the following: In storyboard (where the UIViewController was originally created) I have selected the UIVC in question and in attributes inspector I deselected 'Under Top Bars' and 'Under Bottom Bars' and this appears to have fixed the first part of the animation problem. Now, when I tap in the search bar, the search becomes active but the searchBar does NOT resize. However, when I cancel the searchBar the searchBar still animates and resizes as per the last image in my screenshot. What could be causing THAT resizing?
i've been banging my head on this for too freakin long... props to this dude here for the clues.
if you want your UISearchBar to be in the header along with other views and to scroll with the header instead of sticking at the top, then you've got to remove it and re-add it after the search completes. to be clear, build your header like you already do and then throw this in there as well to handle the screwy animations.
-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
[self.mySearchBar removeFromSuperview];
self.mySearchBar.frame = CGRectMake(0, 0, 320, 44);
[self.table.tableHeaderView addSubview:self.mySearchBar];
}
As Timothy Moose notes here, "It seems that UISearchDisplayController makes an undocumented assumption that the search bar is the only content of the header view"
...yay apple.
Ok, after hours of reading up and trying different things I think I have found out how to have a UISearchBar where I want it and it seems to work ok. I believe the problem was to do with either having multiple views in the tableHeaderView OR the UISearchBar did not like being with another view inside a 'containing' view.
Here is the code I ended up with that allowed me to have a segmentedControl at the top of my UITableView AND a UISearchBar which actually sat at the top of my UIView (to which the UITableView was added).
-(void)loadTableView
{
usableSpace = [[UIScreen mainScreen] applicationFrame];
usableWidth = usableSpace.size.width;
usableHeight = usableSpace.size.height;
_tableView = [[UITableView alloc] init];
[_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
[_tableView setDataSource:self];
[_tableView setDelegate:self];
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
[_searchBar setShowsCancelButton:YES];
NSLog(#"searchBar height = %fl", _searchBar.frame.size.height);
segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Title", #"Date/Time", #"Speaker", nil]];
segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(sortList) forControlEvents:UIControlEventValueChanged];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
[headerView addSubview:segmentedControl];
_tableView.tableHeaderView = headerView;
[self.view addSubview:_tableView];
[self.view addSubview:_searchBar];
self.tableView = _tableView;
}
This meant that I had:
UIViewController with a UINavigationController, UISearchBar, UITableView. The UITableView had a segmentedConrol. The UISearchBar sits just under the UINavigationController and is always present. The UISearchBar sits at the top of the UITableViewController and scrolls with the UITableViewCells.
If anyone knows why the UISearchBar acted as it did in the question then I would be grateful if they could leave a comment. I know that this problem (or very similar) has been experienced by MANY people and I haven't found a definitive answer anywhere.

Resources