ios - 当键盘存在时如何使 UITextField 向上移动 - 开始编辑?

标签 ios objective-c uiscrollview uitextfield uikeyboard

使用 iOS SDK:

我有一个带有 UITextFieldUIView 可以调出键盘。我需要它能够:

  1. 允许滚动 UIScrollView 的内容,以便在调出键盘后查看其他文本字段

  2. 自动“跳转”(通过向上滚动)或缩短

我知道我需要一个 UIScrollView。我已尝试将我的 UIView 类更改为 UIScrollView,但我仍然无法向上或向下滚动文本框。

我需要 UIViewUIScrollView 吗?一个会进入另一个吗?

为了自动滚动到事件文本字段需要实现什么?

理想情况下,尽可能多的组件设置将在 Interface Builder 中完成。我只想为需要的代码编写代码。

注意:我正在使用的 UIView(或 UIScrollView)是由一个标签栏(UITabBar)调出的,它需要正常运行。


我正在为键盘出现时添加滚动条。尽管不需要,但我觉得它提供了更好的界面,因为用户可以滚动和更改文本框等。

当键盘上下移动时,我可以更改 UIScrollView 的框架大小。我只是在使用:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    //Keyboard becomes visible
    scrollView.frame = CGRectMake(scrollView.frame.origin.x,
                                  scrollView.frame.origin.y,
    scrollView.frame.size.width,
    scrollView.frame.size.height - 215 + 50);   // Resize
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    // Keyboard will hide
    scrollView.frame = CGRectMake(scrollView.frame.origin.x,
                                  scrollView.frame.origin.y,
                                  scrollView.frame.size.width,
                                  scrollView.frame.size.height + 215 - 50); // Resize
}

但是,这不会自动“向上移动”或将下方文本字段置于可见区域的中心,这正是我真正想要的。

最佳答案

  1. 如果您现在拥有的内容不适合 iPhone 屏幕,您将只需要一个 ScrollView。 (如果您添加 ScrollView 作为组件的父 View 只是为了让 TextField 在键盘出现时向上滚动,则不需要。)

  2. 防止 TextField 被键盘覆盖的标准方法是在显示键盘时向上/向下移动 View 。

下面是一些示例代码:

#define kOFFSET_FOR_KEYBOARD 80.0

-(void)keyboardWillShow {
    // Animate the current view out of the way
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)keyboardWillHide {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if ([sender isEqual:mailTf])
    {
        //move the main view, so that the keyboard does not hide it.
        if  (self.view.frame.origin.y >= 0)
        {
            [self setViewMovedUp:YES];
        }
    }
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view

    CGRect rect = self.view.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;

    [UIView commitAnimations];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

关于ios - 当键盘存在时如何使 UITextField 向上移动 - 开始编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48741119/

相关文章:

ios - 如何在 UInt8 数组中存储负数

objective-c - 如何在Cocoa中构建OSXFUSE.framework?

长表格的 iOS AutoLayout

iphone - 为什么在此方法声明中没有*?

ios - 如何为forControlEvents 放置多个控制事件?

ios - 使用 UIImage 放大 ScrollView

iphone - 如何使用scrollRectToVisible滚动到图像的中心?

jquery - iOS Chrome 给出了错误的窗口高度

ios - 缺少 IAP Apple 拒绝

ios - 用于存储图像并从存储的缓存中检索的缓存不起作用 - iOS Swift