2017-07-28 62 views
-1

我有一个表格在VC。在表单中有一些字段和文字浏览。当用户点击文本区域键盘弹出窗口,并且用户开始键入它时。但问题在于textview隐藏在键盘后面,用户看不到他/她正在写什么。有没有办法让用户无法解决这个问题?屏幕看起来像这样 enter image description here调整键盘以显示其背后的内容在IOS

+0

使用TPKeyboardAvoiding库。 – KKRocks

+0

我有同样的问题。让你查看可滚动,这样你可以滚动你的视图,直到textView可见。这就是我解决问题的方法。干杯! –

回答

0

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

如果你现在的内容不适合在iPhone屏幕上,你将只需要一个ScrollView。 (如果您将ScrollView添加为组件的超级视图,只是为了在键盘弹出时使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]; 
} 
0

把你的形式进入自己的看法,然后只需在键盘出现时调整视图的底部约束/消失。

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

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

并实行keyboardWillShow提高键盘鉴于上述约束出现时:

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 
} 

上面的代码只会滑动键盘上方的整个视图,但如果您想上下滑动视图以使每个文本字段位于键盘的正上方(并且会针对每个文本字段进行调整),那么只需设置出现键盘时相对于textField的frame.origin.y的底部约束。

相关问题