简体   繁体   English

iPhone - 键盘隐藏TextField

[英]iPhone - Keyboard hides TextField

I am using UITextField to receive user inputs. 我正在使用UITextField来接收用户输入。 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 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. 它会自动检查Textfield的位置,如果键盘隐藏它,它会根据需要自动向上移动。


- (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. 要在键盘出现时滚动,我喜欢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. 我用代码完成了后者,但本教程使用Interface Builder。

Below code works perfect for me . 下面的代码对我来说很完美。

[textFieldFocused becomeFirstResponder];
[self.view addSubview:startView];
[textFieldFocused resignFirstResponder];

You'll have to do this one manually. 您必须手动执行此操作。 Check out this answer. 看看这个答案。 UITextField: move view when keyboard appears UITextField:键盘出现时移动视图

if someone needs it, i`ve translated the solution of ValayPatel to swift 如果有人需要它,我已经将ValayPatel的解决方案翻译成了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()

    }

}

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. 适用于横向模式,但仅适用于iphone。 For iPad, change the frame settings accordingly. 对于iPad,请相应更改框架设置。 The textFieldShouldReturn: method will make the keyboard hide when return is pressed. textFieldShouldReturn:方法将在按下返回时使键盘隐藏。 Hope this helps... 希望这可以帮助...

This situation kind of sucks on the iPhone. 这种情况在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. 我建议您查看一些Apple应用程序,如“联系人”或“设置”,以了解他们如何处理此问题。

Also, I'm sure the iPhone HIG has something to say about it. 另外,我确定iPhone HIG有话要说。

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. 如果您使用UITableView控制器而不是具有tableView作为对象的UIViewController,则不会出现此问题。

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 对于基本的单屏应用程序,这个解决方案就像一个魅力,实现起来非常简单: 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 ! 将UITableView设置为编辑模式很简单!

- (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: 如果你想隐藏一个删除冒泡,在单元格的左边,然后实现一个UITableViewDelegate方法:

- (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... 请在您的视图controller.m文件中使用此代码。当您单击文本字段时,使用此代码键盘将出现。当您单击视图时,键盘将隐藏..我希望这有助于您...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM