Programmatically align a toolbar on top of the iPhone keyboard
As of iOS 3.2 there's a new way to achieve this effect:
UITextFields
and UITextViews
have an inputAccessoryView
property, which you can set to any view, that is automatically displayed above and animated with the keyboard.
Note that the view you use should neither be in the view hierarchy elsewhere, nor should you add it to some superview, this is done for you.
toolbar on top of the keyboard with firstresponder
Try moving the -becomeFirstResponder
call to -viewWillAppear:animated:
or -viewDidAppear:animated:
. I think -viewDidLoad
is usually called just before the view's actually added to the view hierarchy.
Objective c toolbar above keyboard
Only one keyword to search: inputAccessoryView
.
Keep iPhone keyboard-aligned toolbar visible through first responder change?
You need to make a BOOL ivar to keep track of whether or not the keyboard is already visible, so you'll make good use of all of the UITextField delegate methods :) Toolbars with multiple text fields can be tricky, but you're very close!
Add a done button to the top of the keyboard
Please try this code
//set up a placeholder variable for the textfield user typing
UITextView *currentTextView;
-(void)addDoneToolBarToKeyboard:(UITextView *)textView
{
UIToolbar* doneToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
doneToolbar.barStyle = UIBarStyleBlackTranslucent;
doneToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonClickedDismissKeyboard)],
nil];
[doneToolbar sizeToFit];
textView.inputAccessoryView = doneToolbar;
}
//remember to set your text view delegate
//but if you only have 1 text view in your view controller
//you can simply change currentTextField to the name of your text view
//and ignore this textViewDidBeginEditing delegate method
- (void)textViewDidBeginEditing:(UITextView *)textView
{
currentTextView = textView;
}
-(void)doneButtonClickedDismissKeyboard
{
[currentTextView resignFirstResponder];
}
and add this in your view did load
[self addDoneToolBarToKeyboard:self.textView];
Hope that helps
How to make programmatically a toolBar with next and previous on keyboard to navigate between editable fields?
https://github.com/simonbs/BSKeyboardControls
this control can show a toolbar above the keyboard when editing a textfield like this
Add UILabel to UIToolbar Programmatically
EDITED ANSWER
The problem is with the label.
After instanciation, you must draw it with CGRect. so,
add for exemple : label.frame = CGRect(x: 20, y:20, width: 100, height: 50)
Result:
let label = UILabel()
label.text = "Example"
label.frame = CGRect(x: 20, y:20, width: 100, height: 50)
Related Topics
Delete Keychain Items When an App Is Uninstalled
How to Create a Guid/Uuid Using Ios
Uicollectionview, Full Width Cells, Allow Autolayout Dynamic Height
How to Disable Copy Paste Option from Uitextfield Programmatically
Using Custom Fonts in Wkwebview
How to Display Image in iOS Push Notification
Codesign Error: Provisioning Profile Cannot Be Found After Deleting Expired Profile
Cut Transparent Hole in Uiview
React Native: How to Select the Next Textinput After Pressing the "Next" Keyboard Button
Manual Language Selection in an Ios-App (Iphone and Ipad)
How to Use Uivisualeffectview to Blur Image
Programmatically Create a Uiview With Color Gradient
Add "...Read More" to the End of Uilabel
Changing Specific Text's Color Using Nsmutableattributedstring in Swift