[英]IOS: Move TextField Up When Keyboard Appears
我使用以下方法在鍵盤出現時將視圖向上移動,以便鍵盤不會阻止文本字段。 基本上,我將文本字段嵌入到滾動視圖中,並在出現鍵盤時向上滾動。
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
[_scrollView scrollRectToVisible:_activeField.frame animated:YES];
}
}
但是,當文本字段已經位於視圖的頂部並且不需要向上跳轉時,上面的代碼將導致其跳出視圖。
我記得在某處您可以添加一行來防止這種情況,但我不記得了。 基本上,它將測試文本字段是否已經足夠高到足以使鍵盤無法覆蓋它,因此不需要移動視圖。
上面代碼中的if語句似乎並未篩選出未隱藏文本字段的情況。
有人可以建議一種方法嗎?
謝謝。
如果使用自動布局,則應使用約束而不是框架來操縱視圖。 您可以根據需要嘗試操縱textField的底部間距或查看其超級視圖,然后將layoutIfNeeded
調用放入動畫塊中。 您還可以檢查底部約束的值是否已經是特定值,或者在UIKeyboardWillHideNotification
和UIKeyboardWillShowNotification
上傳遞布爾值(如果是),則約束的值不應更改。 就像是:
-(void)showViewAnimatedly:(BOOL)show{
if(show){
[bottomConstraint setConstant:0];
}else{
[bottomConstraint setConstant:-160];
}
[UIView animateWithDuration:0.3f animations:^{
[self.view layoutIfNeeded];
}];
}
我還建議您使用UIKeyboardWillShowNotification
而不是UIKeyboardDidShowNotification
因為過渡似乎與鍵盤的出現更加UIKeyboardDidShowNotification
。 UIKeyboardDidShowNotification
將在出現鍵盤后開始過渡。
簽出“ IQKeyboardManager”。 它是滿足您要求的絕佳控件。 而且您只需要一行代碼。 它適用於UITextFields和UITextViews。 它還具有上一個和下一個按鈕的選項。 您可以將它作為cocoapod安裝在項目中
它很簡單,當需要出現鍵盤時,可以通過動畫更改文本視圖或文本字段的y軸坐標。 您可以參考的代碼如下---->
-(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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.