使用iOS SDK:

我有一台UIView,上面有UITextField,可以弹出一个键盘.我需要它能够:

  1. 打开键盘后,允许滚动UIScrollView的内容以查看其他文本字段

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

我知道我需要UIScrollView分.我试着把我的UIView级改成UIScrollView级,但还是不能上下滚动文本框.

我需要UIView分和UIScrollView分吗?其中一个会进入另一个吗?

要自动滚动到活动文本字段,需要实现哪些操作?

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

注意:我正在使用的UIView(或UIScrollView)是由一个tabbar(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作为组件的SuperView只是为了使TextField在键盘出现时向上滚动,则不需要它.)

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

以下是一些示例代码:

#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相关问答推荐

带有动画层的UIView表示不会在工作表中设置动画

如何使视图完全适合屏幕,而不会在旋转时溢出手机屏幕的边界?

如何在SwiftUI中拥有基于操作系统版本的条件修饰符?

验证开发人员应用程序证书是否已在您的设备上验证

一堆UIGraphics方法在iOS 17中被弃用,没有明确的替代方法?

objc[25442]:Swift类的扩展和类别不允许有+load方法

Swift Combine和iOS版本限制?

在 Swift 中,对象如何被准确地从内存中移除?

创建方案时运行 pod install 时出错

SwiftUI HStack 文本字段对齐

发生异常:需要 issuerId

如何在 SwiftUI 中将选项卡放在滚动视图中?

react 在 android 上运行的本机应用程序,但在 iOS 上出现问题,详细信息在描述中共享

无法初始化 FirebaseApp - Flutter

UIViewControllerRepresentable 在 Xcode 14 iOS 16 上崩溃

Swift 共享数据模型在页面之间进行通信.这个怎么运作

SignalR 永远不会在 iOS 上使用 Xamarin Forms 启动

iOS 10 错误:UICollectionView 接收到具有不存在索引路径的单元格的布局属性

SwiftUI - 列表行中的多个按钮

iOS7 UITextView contentsize.height 替代