iPhone - 键盘隐藏TextField

时间:2010-02-21 19:42:04

标签: iphone keyboard uitextfield

我正在使用UITextField来接收用户输入。但是,由于我的文本字段朝向笔尖的中间/底部,因此当键盘弹出时会隐藏它。有没有什么方法可以将它与键盘一起滑动,以便在选择时它位于键盘的顶部?此外,由于我也在使用数字键盘,是否有一种简单的方法可以在某处包含完成按钮?

感谢您的帮助。

12 个答案:

答案 0 :(得分:26)

我已经为这个问题做了一个简单的应用程序。它会自动检查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;
}

答案 1 :(得分:17)

要在键盘出现时滚动,我喜欢Cocoa With Love的this tutorial

要关闭数字键盘,您可以put a custom "Done" button on the keypad或在屏幕的其余部分上隐藏按钮。我用代码完成了后者,但是this tutorial使用了Interface Builder。

答案 2 :(得分:3)

您必须手动执行此操作。看看这个答案。 UITextField: move view when keyboard appears

答案 3 :(得分:3)

使用此代码:

- (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;
}

也适用于横向模式,但仅适用于iphone。对于iPad,请相应更改框架设置。按下返回时,textFieldShouldReturn:方法将使键盘隐藏。希望这会有所帮助...

答案 4 :(得分:3)

下面的代码非常适合我。

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

答案 5 :(得分:3)

如果有人需要,我已将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()

    }

}

答案 6 :(得分:1)

这种情况在iPhone上很糟糕。您应该做的是设计界面,使键盘出现时字段可见,或者在键盘出现时将字段向上移动。 (当你完成时退缩)

我建议您查看一些Apple应用,例如“通讯录”或“设置”,了解他们如何处理此问题。

另外,我确定iPhone HIG有话要说。

答案 7 :(得分:0)

我知道我回答这个问题的时间有点迟,但我找到了一个非常简单的解决方案。如果您使用UITableView控制器而不是具有tableView作为对象的UIViewController,则不会出现此问题。

当您单击表格底部的文本字段时,键盘会弹出,表格视图会自动向上滚动,直到可以看到正在编辑的文本字段。

但是,当我们使用键盘上的返回按钮时,自动滚动功能不起作用。在这种情况下,我们手动滚动表格以查看文本字段。

我希望这会有所帮助

答案 8 :(得分:0)

我一直在搜索类似问题的无数答案。对于基本的单屏应用程序,此解决方案就像魅力一样,实现起来非常简单:https://github.com/michaeltyson/TPKeyboardAvoiding

当然有更优雅的解决方案,但这将完成工作并快速完成。

答案 9 :(得分:0)

将UITableView设置为编辑模式很简单!

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.tableView setEditing:YES];
}

如果你想在一个单元格的左边隐藏一个删除冒泡,那么实现一个UITableViewDelegate方法:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    return NO;
}

答案 10 :(得分:0)

- (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];
}

答案 11 :(得分:0)

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField:textField up:YES];
}

请在您的视图controller.m文件中使用此代码。当您单击文本字段时,使用此代码键盘将出现。当您单击视图时,键盘将隐藏..我希望这对您有所帮助。 ..