Including UISearchBar in toolbar in iOS
As far as I know unless you are using IPAD for your development, you can not add UISearchBar
directly in UIToolBar
in IPHONE , you need to add the UISearchBar to a customView first and then add it to the toolbar programatically
// your searchbar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(xposition, yposition, width, height)];
//create a customview and make its frame equal to your searchbar
UIView *searchBarView = [[UIView alloc] initWithFrame:searchBar.frame];
// add your searchbar to the custom view
[searchBarView addSubview:searchBar];
//finally add it to your bar item of the toolbar
UIBarButtonItem *searchBarItem =
[[UIBarButtonItem alloc] initWithCustomView:searchBarView];
Resizing UIToolbar when UISearchBar becomes active
Here is my answer to a similar question posted here
The key to a correct animation is to specify UIViewAnimationOptionLayoutSubviews
as an option for the animation.
UISearchBar with UISearchDisplayController animates outside screen
You can animate the frame of your UISearchBar
in the searchDisplayControllerWillBeginSearch
method to correct its position like this:
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
// animate the search bar to the left ie. x=0
[UIView animateWithDuration:0.25f animations:^{
CGRect frame = controller.searchBar.frame;
frame.origin.x = 0;
controller.searchBar.frame = frame;
}];
// remove all toolbar items if you need to
[self.toolbar setItems:nil animated:YES];
}
and the animate it back again when finished searching:
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
// animate search bar back to its previous position and size
// in my case it was x=55, y=1
// and reduce its width by the amount moved, again 55px
[UIView animateWithDuration:0.25f
delay:0.0f
// the UIViewAnimationOptionLayoutSubviews is IMPORTANT,
// otherwise you get no animation
// but some kind of snap-back movement
options:UIViewAnimationOptionLayoutSubviews
animations:^{
CGRect frame = self.toolbar.frame;
frame.origin.y = 1;
frame.origin.x = 55;
frame.size.width -= 55;
controller.searchBar.frame = frame;
}
completion:^(BOOL finished){
// when finished, insert any tool bar items you had
[self.toolbar setItems:[NSArray arrayWithObjects: /* put your bar button items here */] animated:YES];
}];
}
I have answered a similar question, you can check it here, I've put some images too.
The only thing you have to do is to adapt the code for the iPad.
UISearchBar: weird expanding animation
Here is my answer to a similar question posted:here and here
The key to a correct animation is to specify UIViewAnimationOptionLayoutSubviews
as an option for the animation.
Here is a my full answer to those similar questions:
Usually you don't want to put a search bar inside a toolbar, however, it seems you want to do something similar to what I did.
So here is how I did it, it may be called a hack, but it works like a charm :)
First you have to set it up in interface builder like this:
Notice that the search is not a child of toolbar, instead it is above.
The search bar should have "clear color" background and flexible left, right and width autoresizing masks.
You put a 1-pixel label with black background below the toolbar, ie. [x=0, y=44, width=320 or frame width, height=1], also flexible left, right and width autoresizing masks.This is to hide the one visible pixel you get, after the search display controller shows the table view. Try it without it to understand what I mean.
You setup any tool bar items and be sure to have outlets for them, since you will be needing those.
and now for the code ...
when you start searching:
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
// animate the search bar to the left ie. x=0
[UIView animateWithDuration:0.25f animations:^{
CGRect frame = controller.searchBar.frame;
frame.origin.x = 0;
controller.searchBar.frame = frame;
}];
// remove all toolbar items
[self.toolbar setItems:nil animated:YES];
}
when you end searching
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
// animate search bar back to its previous position and size
// in my case it was x=55, y=1
// and reduce its width by the amount moved, again 55px
[UIView animateWithDuration:0.25f
delay:0.0f
// the UIViewAnimationOptionLayoutSubviews is IMPORTANT,
// otherwise you get no animation
// but some kind of snap-back movement
options:UIViewAnimationOptionLayoutSubviews
animations:^{
CGRect frame = self.toolbar.frame;
frame.origin.y = 1;
frame.origin.x = 55;
frame.size.width -= 55;
controller.searchBar.frame = frame;
}
completion:^(BOOL finished){
// when finished, insert any tool bar items you had
[self.toolbar setItems:[NSArray arrayWithObject:self.currentLocationButton] animated:YES];
}];
}
With this I get the following with a nice animation :)
UISearchBar with a left button
for( id view in search.subviews) {
if([view isKindOfClass:[UIImageView class]]) {
UIImageView *img = (UIImageView *)view;
img.image = nil;
}
}
search.backgroundImage = [UIImage imageNamed:@"searchbar.png"];
search is the object for UISearchbar
Related Topics
How to Create a Uiimage with Uibezierpath
iOS App Getting Throttled from Local Searches
Xcode Creates Wrong IPA Folder Structure
Swift - Add Gesture Recognizer to Object in Table Cell
Game Center Login Dialog Not Shown Again After Cancelling It for the First Time (Ios7)
Capture Location in All States App
Changing Iphone's Date and Time
How to Ignore Certain Uitouch Points in Multitouch Sequence
How to Sort 1 Array in Swift/Xcode and Reorder Multiple Other Arrays by the Same Keys Changes
Uiview Drawrect: Draw the Inverted Pixels, Make a Hole, a Window, Negative Space
iOS Notification When Application Is Closed
Xcode 7 Beta 6, Dyld _Nsarray0_ Crash
How to Default Uilabel Font and Size Using Swift
Objective C- Trouble Updating UI on Main Thread
A Lighter Way of Discovering Text Writing Direction
How to Detect Fullscreen Mode Using Avplayerviewcontroller in Swift
Storing [Nsnull Null] Values in Nsuserdefaults, from JSON Serialization, Causes Unwanted Exceptions