繁体   English   中英

iPhone - 键盘隐藏TextField

[英]iPhone - Keyboard hides TextField

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

谢谢您的帮助。

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

要在键盘出现时滚动,我喜欢Cocoa With Love的这个教程

要关闭数字键盘,您可以在键盘上放置一个自定义的“完成”按钮,或在屏幕的其余部分上设置一个隐形按钮。 我用代码完成了后者,但本教程使用Interface Builder。

下面的代码对我来说很完美。

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

您必须手动执行此操作。 看看这个答案。 UITextField:键盘出现时移动视图

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

    }

}

使用此代码:

- (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:方法将在按下返回时使键盘隐藏。 希望这可以帮助...

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

我建议您查看一些Apple应用程序,如“联系人”或“设置”,以了解他们如何处理此问题。

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

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

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

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

我希望这有帮助

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

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

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

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.tableView setEditing:YES];
}

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

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

暂无
暂无

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

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