iPhone - Keyboard hides TextField
I am using UITextField to receive user inputs. However, since my textfields are towards the middle/bottom of the nib, it gets hidden when the keyboard pops up. Is there any way sort of slide it along with开发者_如何学Go the keyboard so that it's on the top of the keyboard during selection? Also, since I am also using the numberpad, is there an easy way to include a done button somewhere?
Thanks for the help.
I have made a simple Application for this problem. It automatically checked the Textfield's position and if keyboard hides it , it will automatically move up as per need.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
int animatedDistance;
int moveUpValue = textField.frame.origin.y+ textField.frame.size.height;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = 216-(460-moveUpValue-5);
}
else
{
animatedDistance = 162-(320-moveUpValue-5);
}
if(animatedDistance>0)
{
const int movementDistance = animatedDistance;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
To scroll when the keyboard appears, I like this tutorial from Cocoa With Love.
To dismiss the number keypad, you can put a custom "Done" button on the keypad or make an invisible button over the rest of the screen. I have done the latter with code, but this tutorial uses Interface Builder.
You'll have to do this one manually. Check out this answer. UITextField: move view when keyboard appears
Below code works perfect for me .
[textFieldFocused becomeFirstResponder];
[self.view addSubview:startView];
[textFieldFocused resignFirstResponder];
Use this code:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:self.view.window];
}
- (void)viewDidDisappear:(BOOL)animated {
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
}
- (void)keyboardDidHide:(NSNotification *)n
{
CGRect viewFrame = scrollView.frame;
UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft))
viewFrame.size.height += 140;
else viewFrame.size.height += 216; //portrait mode
scrollView.frame = viewFrame;
keyboardVisible = NO;
}
- (void)keyboardDidShow:(NSNotification *)n
{
CGRect viewFrame = scrollView.frame;
UIDeviceOrientation orientSide = [[UIDevice currentDevice] orientation];
if ((orientSide == UIDeviceOrientationLandscapeRight) || (orientSide == UIDeviceOrientationLandscapeLeft))
viewFrame.size.height -= 140;
else viewFrame.size.height -= 216; //portrait mode
scrollView.frame = viewFrame;
keyboardVisible = YES; }
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
CGRect viewFrame = scrollView.frame;
if ((interfaceOrientation == UIDeviceOrientationLandscapeRight) || (interfaceOrientation == UIDeviceOrientationLandscapeLeft)) //wants to change to landscape mode
if (keyboardVisible == NO)//currently in portrait,check if keyboard was present
viewFrame.size = CGSizeMake(480,250);
else viewFrame.size = CGSizeMake(480,170);
else {//wants to change to portrait mode
if (keyboardVisible == NO)//currently in landscape,check if keyboard was present
viewFrame.size = CGSizeMake(320,416);
else viewFrame.size = CGSizeMake(320,200);
}
scrollView.frame = viewFrame;
return YES;
}
Works for landscape mode too, but only for iphone. For iPad, change the frame settings accordingly. The textFieldShouldReturn:
method will make the keyboard hide when return is pressed. Hope this helps...
if someone needs it, i`ve translated the solution of ValayPatel to swift
func animatedTextField(textField: UITextField, up: Bool){
var animatedDistance : Int = 0
let moveUpValue : Int = Int(textField.frame.origin.y) + Int(textField.frame.size.height)
switch UIDevice.currentDevice().orientation {
case .Portrait , .PortraitUpsideDown:
animatedDistance = 216 - (460 - moveUpValue - 5)
default:
animatedDistance = 162 - (320 - moveUpValue - 5)
}
if (animatedDistance > 0 ){
let movementDistance : Int = animatedDistance
let movementDuration : Float = 0.3
let movement = up ? -movementDistance : movementDistance
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(NSTimeInterval(movementDuration))
self.view.frame = CGRectOffset(self.view.frame, 0, CGFloat(movement))
UIView.commitAnimations()
}
}
This situation kind of sucks on the iPhone. What you are supposed to do is either design your interface in such a way that the fields are visible when the keyboard appears, or shift the field up when the keyboard appears. (And back down when you are done)
I suggest to look at some Apple apps like Contacts or Settings to see how they are dealing with this.
Also, I'm sure the iPhone HIG has something to say about it.
I know I'm a little late in answering this but, I found a very simple solution. If you use a UITableView controller rather than a UIViewController having a tableView as an object in it, this issue does not appear.
When you click the textfield that is at the bottom of the table, the keyboard pops up and the table view automatically scrolls up till the textfield that is being edited is visible.
However the auto scrolling does not work when we use the return button on the keyboard. In this scenario we have manually scroll the table up to see the textfield.
I hope this helps
I have been searching through countless answers to similar questions. For basic single screen apps, this solutions works like a charm and is incredibly simple to implement: https://github.com/michaeltyson/TPKeyboardAvoiding
Certainly there are more elegant solutions, but this will get the job done and quick.
It's simple as setting UITableView to edition mode !
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setEditing:YES];
}
If you would like to hide a delete bubbels, to the left of a cell then implement a UITableViewDelegate Method:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"%f",textField.frame.origin.y);
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin);
[scrollView setContentOffset:scrollPoint animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[scrollView setContentOffset:CGPointZero animated:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
Please use this code in your view controller.m file ..Using this code when you click the text field the keyboard will appear.Again when you click your view the keyboard will be hide ..I hope this is helps you...
精彩评论