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.
Related
I have issue with UIToolbar background color above the keyboard, here the picture: not expected background color on UIToolbar. I expected to see the same color as keyboard background, what I'm doing wrong?
I have this code:
UIToolbar* toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
UIBarButtonItem *spaceBarButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil]; // to make close button on right side
UIBarButtonItem *closeBarButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemClose
target:self action:#selector(endEditing:)];
toolbar.items = #[spaceBarButton, closeBarButton];
textField.inputAccessoryView = toolbar;
So It seems I found solution its kinda hack, but still working.
UIInputView on init have inputViewStyle which mimics the keyboard background. If use UIInputView as parent view for UIToolbar with a clear background color, we will be good. Or use UIInputView itself, and add buttons on it, and apply it on inputAccessoryView without UIToolbar.
It depends on your content and future needs.
Example of keyboard what will shows: toolbar with right color implemented
My example with UIToolbar as subclass you can implement variant you want:
//add toolbar
UIToolbar* toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];//get height for toolbar
CGFloat width = [UIScreen mainScreen].bounds.size.width;//set width for toolbar
CGRect frame = toolbar.frame;
frame.size.width = width;
toolbar.frame = frame;
//toolbar buttons setting
UIBarButtonItem *spaceBarButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil]; // to make close button on right side
UIBarButtonItem *closeBarButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemClose
target:textField action:#selector(endEditing:)];
toolbar.items = #[spaceBarButton, closeBarButton];
//make toolbar background clear color
[toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
[toolbar setBackgroundColor:[UIColor clearColor]];
//creating UIInputView for using its background color
UIInputView *inputView = [[UIInputView alloc] initWithFrame:toolbar.bounds inputViewStyle:UIInputViewStyleKeyboard];
// UIInputViewStyleKeyboard - view mimics the keyboard background
[inputView addSubview:toolbar];
textField.inputAccessoryView = inputView;
In my project, some viewControllers have multiple text fields,
I found how to add a toolbar above keyboard with a "Ok" button to hide keyboard when the button is tapped.
The code I am using is this below :
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:_destField action:#selector(resignFirstResponder)];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 34)];
toolbar.items = [NSArray arrayWithObjects:flex, barButton, nil];
_destField.inputAccessoryView = toolbar;
How can I easily reuse this code in the same view controller?
The "target" makes this difficult, is there a way without creating a toolbar for each textField?
Thanks!
If this code is in a view controller then there is a simple solution. Change the barButton to something like this:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissKeyboard)];
Then add this method to the view controller:
- (void)dismissKeyboard {
[self.view endEditing:YES];
}
That will dismiss the keyboard no matter what view is showing it.
Now you can reuse that toolbar as the inputAccessoryView for any text field/view in the view controller.
I want to add a "Done" button and a segmented control to a Decimal Pad keyboard. Ideally I would like to lay out a keyboard accessory view with Auto Layout in Interface Builder.
Is this even possible? Do I create a new XIB or can I do it in the existing Storyboard scene somehow? How do I attach the accessory view to the appropriate text field?
Would you mind doing it programmatically?
Typically you take a UIToolbar to a UITextField with your items, but you may need to subview the UISegmentedControl;
UIToolbar *keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:...
// Customize segmentedControl's properties here.
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(doneButtonPressed)];
[keyboardToolbar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, nil]];
[keyboardToolbar addSubview:segmentedControl];
[textField setInputAccessoryView:keyboardToolbar];
EDIT: It is possible to create the toolbar initially in IB, but you have to drag it off the view and to the left hand sidebar containing the scene and link it to a reference outlet, then assign it with setInputAccessoryView in your viewDidLoad.
How can I add a toolbar on the left side of the splittview?
I have a navigationcontroller
and i already tried this:
UIToolbar *toolbar = [[UIToolbar alloc] init]; toolbar.barStyle =
UIBarStyleDefault;
[toolbar sizeToFit];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:#"back" style:UIBarButtonItemStyleBordered target:self
action:#selector(info_clicked:)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
[self.navigationController.view addSubview:toolbar];
But it is noch working. It appears at the top and not at the bottom
MEMA
You don't have to create your own toolbar for a UINavigationController, just set toolbarHidden to NO to use its built-in one and add your UIBarButtonItem to it.
I am new to iOS development.I have create a navigation bar in my iPad application view.I don't need navigation controller that's why i have added only navigation bar. Now i want to add button in that navigation bar.I tried a lot but no success. Is it possible to add only navigation bar with button ? If yes then suggest me some sample code.
"i don't have navigation controller or need it. i just want to add navigation bar in only one view."
Bellow is my code which i write for adding navigation bar in ViewDidLoad()
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1026, 50)];
[navBar setTintColor:[UIColor blackColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];
Thanks in advance....
I used the Interface Builder to make a static tableView in a UITableViewController.
This UITableViewController is shown modally. Then I added a NavigationBar without the UINavigationController behind it as follows:
//Creating the plain Navigation Bar
UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
//The UINavigationItem is neede as a "box" that holds the Buttons or other elements
UINavigationItem *buttonCarrier = [[UINavigationItem alloc]initWithTitle:#"Sign-In"];
//Creating some buttons:
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Zurück" style:UIBarButtonItemStyleDone target:self action:#selector(signInBackPressed:)];
UIBarButtonItem *barDoneButton = [[UIBarButtonItem alloc] initWithTitle:#"Fertig" style:UIBarButtonItemStylePlain target:self action:#selector(signInDonePressed:)];
//Putting the Buttons on the Carrier
[buttonCarrier setLeftBarButtonItem:barBackButton];
[buttonCarrier setRightBarButtonItem:barDoneButton];
//The NavigationBar accepts those "Carrier" (UINavigationItem) inside an Array
NSArray *barItemArray = [[NSArray alloc]initWithObjects:buttonCarrier,nil];
// Attaching the Array to the NavigationBar
[headerView setItems:barItemArray];
// Adding the NavigationBar to the TableView
[self.tableView setTableHeaderView:headerView];
I hope this helps somebody!
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editButton)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f];
self.navigationItem.rightBarButtonItem = bi1;
[bi1 release];
You can add buttons to navigation bar as follows:
UIBarButtonItem *btnSave = [[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(save_Clicked:)];
navBar.rightBarButtonItem = btnSave;
[btnSave release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]
initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cancel_Clicked:)];
navBar.leftBarButtonItem = btnCancel;
[btnCancel release];