2017-07-28 83 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的底部約束。

相關問題