I'm using a numeric keyboard for input from a UITextField. I noticed that Interface Builder has a return key property called "Done". When I select "Done" under the return key options, I do not see that a Done button is created on the numeric keyboard. I see the "Done" button on other keyboard types if I change it from a numeric keyboard to something else, but not when using a numeric keyboard.
I want to create a "Done" button on the keyboard which will hide the keyboard when pressed
I want to do this because of the amount of real estate the keyboard takes up
I'm very new to iOS development, so the easiest and least convoluted way to accomplish this goal would be appreciated
Thanks in advance!
The typical way of doing this is to assign a toolbar containing a button to the inputAccessoryView property of the text view.
I found this solution online but it implies making an image for your done button: http://www.neoos.ch/blog/37-uikeyboardtypenumberpad-and-the-missing-return-key
Also you can use the same method for getting the UIKeyboard view showed in the example and do the following, as per #Jim's suggestion:
UIToolbar *keyboardViewToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 206, 320, 44)];
UIBarButtonItem *keyboardDoneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneSettingDate)];
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithTitle:#"Enter Number" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *spacer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
NSArray *barButtonItems = [[NSArray alloc] initWithObjects:spacer1, title, spacer2, pickerDoneButton, nil];
[keyboardViewToolbar setItems:barButtonItems];
And add that view to the keyboard view.
Also, as a separate suggestion considering you're just starting out. On iTunes U you can download the Developing Applications for iOS Stanford class, it's a very complete course on iOS development, available for free on the iTunes Store.
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
UIToolbar* KeyBoardToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
KeyBoardToolbar.barStyle = UIBarStyleBlackTranslucent;
KeyBoardToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:[GlobalSettings GetTextBySelectedLocale:#"Done"] style:UIBarButtonItemStyleBordered target:self action:#selector(DismissKeyBoard)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
nil];
[KeyBoardToolbar sizeToFit];
txtMessage.inputAccessoryView = KeyBoardToolbar;
}
Where txtMessage could be UiTextField or UITextView.
-(void)DismissKeyBoard{
[self.view endEditing:YES];
}
Related
How to make programmatically a toolBar with next and previous on keyboard to navigate between editable fields (Textfield, Textarea, Datepicker, Pikerview ) ?
https://github.com/simonbs/BSKeyboardControls
this control can show a toolbar above the keyboard when editing a textfield like this
UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:#"Prev" style:UIBarButtonItemStyleBordered target:self action:#selector(prevButtonPressed:)];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextButtonPressed:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)]
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[toolbar setItems:#[prevButton, nextButton, flexibleSpace, doneButton]];
UITextView *textView = [[UITextView alloc] inithWithFrame:CGRectMake(x, y, width, height)];
textView.inputAccessoryView = toolbar;
You can use a UISegmentedControl to create the Prev/Next effect similar to the one posted by #adali
I hope to push keyboard with button list on the top of the keyboard.
Please refer to the image
Is it possible?
you need to add a toolbar. Here is an example I used for an action sheet - you can make the modifications for the keyboard easily:
//DEFINING TOOL BAR
UIToolbar * keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
keyboardToolbar.barStyle = UIBarStyleBlackOpaque;
[keyboardToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissKeyboard:)];
[barItems addObject:doneBtn];
[keyboardToolbar setItems:barItems animated:YES];
[keyboard addSubview:keyboardToolbar];
You can obviously add more items to the UIToolBar for "Next", "Previous", etc.
It is possible. Here is a link to a keyboard controls project on github.
how can i add buttons just beside a left barButton or just beside rightbarButton,
i mean i need to have more than 2 buttons on the title bar is that possible?
thanx in advance
You can use a view with two buttons in your leftBarButton -
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftBtnsView];
As drawn from this blog post:
Think of a toolbar as your container,
instead of the UIView object. And
since a UIToolBar is UIView based, it
can be added to the right side of a
nav bar using the above trick. The
following code shows how you can add
two standard buttons to the right
side:
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
Also check the SO post
Adding buttons to navigation bar
I would suggest to use UISegmentedControl to hold all your buttons and use
[[UIButton alloc] initWithCustomView:] to show the segment control.
Here is a sample of how you could go about doing this. I am using a MasterDetailView template that comes with the top navigation bar for this example:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] init];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
leftButton.title = #"Left button";
rightButton.title = #"right button";
self.navigationItem.leftBarButtonItem = leftButton;
self.navigationItem.rightBarButtonItem = rightButton;
}
You can do something like this, there's something called UINavigationItem.rightBarButtonItems
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"xxx.png"] style:UIBarButtonItemStyleDone target:self action:#selector(something)];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"xxx.png"]
style:UIBarButtonItemStyleDone target:self
action:#selector(something)];
self.navigationItem.rightBarButtonItems = #[settingsButton, menuButton];
i think you need something like this
UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationController.navigationItem.leftBarButtonItems=#[firstButton, secondButton];
it will add two bar button items on left of navigation bar. you can make it on right side the same way
self.navigationController.navigationItem.rightBarButtonItems=#[firstButton, secondButton];
i've seen a "Previous/Next/Done" bar above the keyboard when entering text in a webview on an iPhone. i want to use this built in bar in an app that i'm making in conjunction with a UITextView. i have seen a very similar looking bar above the keyboard in several apps. my guess is this is either some hand made bar that just looks like the bar i want or the programmer figured out a way to show the actual bar. assuming its the actual bar and not a hand made bar how do you get it to display?
There's no built-in Prev/Next/Done bar, you have to create it yourself.
SEE ALSO:
Programatically align a toolbar on top of the iPhone keyboard
How to find out what UITextField caused a UIKeyboardWillShowNotification?
I wrote XCDFormInputAccessoryView which is an input accessory view with previous/next/done buttons.
Create this method and call it on ViewWillLoad:
- (void) keyboardToolbarSetup
{
if(self.keyboardToolbar==nil)
{
self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(anyAction)];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(anyOtherAction)];
NSArray *toolbarButtons = [[NSArray alloc]initWithObjects:cancelButton,extraSpace,doneButton, nil];
[self.keyboardToolbar setItems:toolbarButtons];
self.myTextView.inputAccessoryView=self.keyboardToolbar;
}
}
How would I be able to set the position of a UIBarButtonItem? Like, I would like to set it to be on either the very right of a UIToolbar or the very left depending on a state.
Thank you.
You do not set directly the position of a UIBarButtonItem in an UIToolbar. Instead you defined the items' order and put flexible space on the left or on the right.
What you can do is:
Create the UIBarButtonItem you want to place (button 1).
Create an UIBarButtonItem of type UIBarButtonSystemItemFlexibleSpace (button 2).
If you want to put the button on the left, create an array with (button 1) and (button 2) and pass it to the UIToolbar by using the setItems:animated: method.
If you want to put the button on the right, create an array with (button 2) and (button 1) and pass it to the UIToolbar by using the setItems:animated: method.
I hope it helps you...
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* PrevButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:105 target:nil action:nil]; //<
UIBarButtonItem* NextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:nil action:nil]; //>
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneClicked:)];
UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fake = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil] ;
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects: PrevButton,fake, NextButton,fake,flexSpace,fake,doneButton,nil] animated:YES];
Link explains Bar Button Item Methods and Arguments
https://developer.apple.com/library..... Use Fake Item to get exact pinch location on Button...