调整键盘以在IOS中显示其后面的内容

调整键盘以在IOS中显示其后面的内容,ios,keyboard,Ios,Keyboard,我有一张vc的表格。表单中有一些字段和文本视图。当用户单击文本区键盘弹出窗口,用户开始在其上键入。但问题是textview隐藏在键盘后面,用户看不到他/她在写什么。是否有任何方式用户无法获得此问题?屏幕看起来像这样 我建议您在键盘出现时向上移动视图,在键盘消失时向下移动视图。如果您想这样做: 只有当您现在拥有的内容不适合iPhone屏幕时,您才需要滚动视图。(如果您将ScrollView添加为组件的superview。只需在键盘打开时使文本字段向上滚动,则不需要它。) 要在不被键盘隐藏的情况下显

我有一张vc的表格。表单中有一些字段和文本视图。当用户单击文本区键盘弹出窗口,用户开始在其上键入。但问题是textview隐藏在键盘后面,用户看不到他/她在写什么。是否有任何方式用户无法获得此问题?屏幕看起来像这样

我建议您在键盘出现时向上移动视图,在键盘消失时向下移动视图。如果您想这样做:

只有当您现在拥有的内容不适合iPhone屏幕时,您才需要滚动视图。(如果您将ScrollView添加为组件的superview。只需在键盘打开时使文本字段向上滚动,则不需要它。) 要在不被键盘隐藏的情况下显示文本字段,标准方法是在显示键盘时,向上/向下移动具有文本字段的视图。 以下是一些示例代码:

#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];
}  
#为键盘80.0定义kOFFSET#u
-(无效)键盘将显示{
//设置当前视图的动画,使其不碍事
if(self.view.frame.origin.y>=0)
{
[self-setViewMovedUp:是];
}
else if(self.view.frame.origin.y<0)
{
[self-setViewMovedUp:否];
}
}
-(无效)键盘将隐藏{
if(self.view.frame.origin.y>=0)
{
[self-setViewMovedUp:是];
}
else if(self.view.frame.origin.y<0)
{
[self-setViewMovedUp:否];
}
}
-(无效)textFieldDidBeginEditing:(UITextField*)发送方
{
如果([发送方isEqual:mailTf])
{
//移动主视图,使键盘不会隐藏它。
if(self.view.frame.origin.y>=0)
{
[self-setViewMovedUp:是];
}
}
}
//方法在显示/关闭键盘时向上/向下移动视图
-(无效)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationDuration:0.3];//如果要向上滑动视图
CGRect rect=self.view.frame;
if(movedUp)
{
//1.向上移动视图的原点,以便隐藏的文本字段位于键盘上方
//2.增大视图的大小,以便覆盖键盘后面的区域。
rect.origin.y-=kOFFSET\u用于键盘;
rect.size.height+=键盘的kOFFSET\u;
}
其他的
{
//恢复到正常状态。
rect.origin.y+=kOFFSET_用于键盘;
rect.size.height-=kOFFSET\u用于键盘;
}
self.view.frame=rect;
[UIView委员会];
}
-(无效)视图将显示:(BOOL)动画
{
[超级视图将显示:动画];
//注册键盘通知
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@选择器(键盘将显示)
名称:UIKeyboardWillShowNotification
对象:无];
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@选择器(键盘将隐藏)
名称:UIKeyboardWillHideNotification
对象:无];
}
-(无效)视图将消失:(BOOL)已设置动画
{
[超级视图将消失:动画];
//在不可见时注销键盘通知。
[[NSNotificationCenter defaultCenter]移除观察者:self
名称:UIKeyboardWillShowNotification
对象:无];
[[NSNotificationCenter defaultCenter]移除观察者:self
名称:UIKeyboardWillHideNotification
对象:无];
}  

将表单放入自己的视图中,然后在键盘出现/消失时简单地调整视图的底部约束

要启用键盘出现时的通知,请将以下行添加到viewDidLoad:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
和implement keyboard将显示,以在出现时将约束提升到键盘视图上方:

func keyboardWillShow(notification: NSNotification) {
    if let info = notification.userInfo {
        let rect: CGRect = info["UIKeyboardFrameEndUserInfoKey"] as! CGRect //get the keyboard frame as CGRect to get its dimensions
        self.view.layoutIfNeeded()

        UIView.animate(withDuration: 0.25, animations: {
            self.formViewBottomConstraint?.constant = rect.height + 14 //replace 14 with your desired pixel offset from the top of the keyboard
            self.view.layoutIfNeeded()
        })
    }
}
最后,当键盘消失时,将视图的底部约束重置为其原始值:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        self.formViewBottomConstraint?.constant = 14
        return true
}

上面的代码只是将整个视图滑动到键盘上方,但如果您想上下滑动视图,使每个文本字段都位于键盘上方(并针对每个文本字段进行调整),然后,当键盘出现时,只需设置相对于文本字段的frame.origin.y的底部约束。

使用TPKeyboardAvoiding library。我也有同样的问题。使视图可滚动,这样可以滚动视图,直到文本视图可见。这就是我解决问题的方法。干杯