I am trying to place a UISearchBar on toolbar in an .xib file. i am able to drag and drop the search bar onto the toolbar but it shows the following error.
ControllerName.xib:error: illegal Configuration: UISearchBar embedded in UIBarButtonItems (Only available ub iPad documents).
Please guide me how to include the UISearchBar into the Toolbar in xib.
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];
By Programetically:
UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[Toolbar sizeToFit];
UISearchBar *testbar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,2,250,38)];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(ButtonMethod)];
[barItems addObject:btnCancel];
[Toolbar setItems:barItems animated:YES];
[Toolbar addSubview:testbar];
[self.view addSubview:Toolbar];
Here is Button Method;
-(void)ButtonMethod
{
// Write Code for ButtonMethod Method
}
You can do this Interface Builder now that Apple fixed this in Xcode 8.2 . I think they disabled it before because popovers were not allowed on iOS before iOS 8.0 and a search bar in a toolbar implies that a popover will be used most times. But they forgot to disable it with iOS 8.0.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.