我有一個ScrollView
其中有TableViews
。我設置鍵盤出現時,鍵盤上方的內容應該移動,所以鍵盤不會遮擋任何東西。只有當鍵盤彈起時,我如何滾動鍵盤上方的內容?如何在鍵盤顯示時在Tabelview中滾動?
-1
A
回答
0
當鍵盤上下移動時,您需要管理高度UITableView。通知將有助於管理各種鍵盤的狀態。根據國家,我們可以udpate UITableView的高度。
-(void) keyboardWillShow:(NSNotification *)note{
// get keyboard size and loctaion
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// Need to translate the bounds to account for rotation.
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
// set views with new info
_containerBottom.constant = keyboardBounds.size.height;
CGRect rectTable = _tblMessages.frame;
rectTable.size.height -= keyboardBounds.size.height;
_tblMessages.frame = rectTable;
[self.view layoutIfNeeded];
[self scrollToBottom];
// commit animations
[UIView commitAnimations];
}
-(void) keyboardWillHide:(NSNotification *)note{
CGRect keyboardBounds;
[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
// animations settings
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:[duration doubleValue]];
[UIView setAnimationCurve:[curve intValue]];
CGRect rectTable = _tblMessages.frame;
rectTable.size.height += keyboardBounds.size.height;
_tblMessages.frame = rectTable;
// commit animations
[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
您可以添加添加觀察者來檢查鍵盤顯示與否。
NotificationCenter.default.addObserver(self,
selector: #selector(viewController.keyboardWillShow(notification:)),
name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(viewController.keyboardWillHide(notification:)),
name: NSNotification.Name.UIKeyboardWillHide, object: nil)
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo? [UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
print("Show")
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
print("Hide")
}
}
然後你需要爲內容做計算表明,當鍵盤將會出現,並設置內容滾動型的偏移量。
相關問題
- 1. 如何在保留滾動活動的同時顯示鍵盤?
- 2. 如何在鍵盤顯示時禁用UIWebView的自動滾動
- 3. 顯示鍵盤時可滾動嗎?
- 4. 鍵盤顯示時UITextView不會滾動
- 5. 當鍵盤顯示時不滾動collectionview
- 6. 如何在顯示searchController時自動顯示鍵盤
- 7. 如何在鍵盤上顯示我的滾動佈局?
- 8. 當顯示軟鍵盤時無法滾動滾動視圖
- 9. 在Ionic 2中,鍵盤顯示時如何浮動鍵盤上方的元素?
- 10. Tableview在鍵盤顯示時滾動內容
- 11. Phonegap在顯示鍵盤時阻止滾動
- 12. EditText在移動時不顯示鍵盤
- 13. 如何在Android中的AutoCompleteTextView的Activity啓動時顯示鍵盤
- 14. 如何在物理鍵盤連接時顯示虛擬鍵盤
- 15. 如何在軟鍵盤顯示時隱藏EditText軟鍵盤?
- 16. 鍵盤顯示滾動TextField向上
- 17. 防止鍵盤顯示滾動iOS 6
- 18. Android:顯示滾動頁面的鍵盤
- 19. 的EditText滾動型顯示鍵盤
- 20. 當鍵盤顯示,滾動有限
- 21. 在移動Safari中顯示鍵盤時防止屏幕向上滾動
- 22. 當顯示鍵盤時將UITextField滾動到視圖中
- 23. 在Windows Mobile中自動顯示鍵盤
- 24. Android-如何在鍵盤啓動時自動滾動
- 25. 在Libgdx中顯示鍵盤
- 26. 如何在使用bootstrap-datepicker時不顯示移動鍵盤?
- 27. 如何讓鍵盤在應用程序啓動時顯示
- 28. 如何在屏幕打開時自動顯示android鍵盤?
- 29. 如何在應用程序啓動時顯示iPad鍵盤?
- 30. 如何在啓動Activity時顯示虛擬鍵盤?