How do I add a line at the top of the keyboard in ios?
I have a view in which I am showing the Numeric Keyboard. My problem is the top line is missing on the keyboard. An unpleasant effect appears on a white background:
How can I add a 1pt view at the top to simulate the top row?
quick and dirty:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 1.0 / [UIScreen mainScreen].scale)];
separatorView.backgroundColor = [UIColor lightGrayColor];
textField.inputAccessoryView = separatorView;
return YES;
}
The easiest way is to add 1p height UIView
as inputAccessoryView
in UITextField
. See Custom Views for Data Entry .
If you are using more than one field, or prefer more control over it, you can add a subview to the current controller view
and position it accordingly. To do this, you use the keyboard on the notice sent to the operating system ( UIKeyboardWillShowNotification
and UIKeyboardWillHideNotification
), as described in How to make the UITextField move up when keyboard is present? ...
For Swift 3, iOS 10
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
var separatorView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 1.0/UIScreen.main.scale))
separatorView.backgroundColor = UIColor.lightGray
textField.inputAccessoryView = separatorView
return true
}