[英]uitableview will not scroll when keyboard appears on the first launch of view. Later it works properly
關於在出現鍵盤時移動UITableView
答案很多,但是我的問題有所不同。 當出現鍵盤時,我能夠移動UITableView
,但是我面臨的唯一問題是,當我第一次訪問該視圖時, UITableView
不會滾動,但是當我按下后退按鈕並重新訪問該視圖時,它才能正常工作。 我很困惑,這是每次我第一次訪問視圖時都期望的相同代碼。 我在xib中使用UITableView。 我嘗試設置contentInset但沒有用。 任何幫助將不勝感激。
我正在發布一張圖像,該圖像顯示了我面臨的問題
下面是我的代碼。
- (void)viewDidLoad
{
[super viewDidLoad];
//keyboard observers
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//tap on tableview or whole view
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.view addGestureRecognizer:gestureRecognizer];
//scroll to bottom of conversation
[self scrollToBottomOfConversation];
}
- (void)scrollToBottomOfConversation
{
CGFloat yOffset = 0;
if (_chatConversationTableView.contentSize.height > _chatConversationTableView.bounds.size.height) {
yOffset = _chatConversationTableView.contentSize.height - _chatConversationTableView.bounds.size.height;
}
[_chatConversationTableView setContentOffset:CGPointMake(0, yOffset) animated:NO];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect frame = CGRectMake(_chatConversationTableView.frame.origin.x,
_chatConversationTableView.frame.origin.y,
_chatConversationTableView.frame.size.width,
_chatConversationTableView.frame.size.height - size.height);
_chatConversationTableView.frame = frame;
CGRect framee = containerView.frame;//containerView is my subView which holds the textbox and send button
framee.origin.y = self.view.frame.size.height - framee.size.height - size.height;
containerView.frame = framee;
[UIView commitAnimations];
//scroll to bottom of conversation
[self scrollToBottomOfConversation];
}
-(void)keyboardWillHide:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
_chatConversationTableView.frame = CGRectMake(_chatConversationTableView.frame.origin.x,
_chatConversationTableView.frame.origin.y,
_chatConversationTableView.frame.size.width,
_chatConversationTableView.frame.size.height + size.height);
CGRect frame = containerView.frame;
frame.origin.y = self.view.frame.size.height - frame.size.height;
containerView.frame = frame;
[UIView commitAnimations];
}
在主線程中運行鍵盤通知代碼解決了我的問題。 這幫助我克服了這個問題。 我認為由於某種原因,當鍵盤出現時,tableview框架的大小沒有更新。 發布代碼,以便對他人有所幫助。
- (void)keyboardWillShow:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
//This code will run in the main thread:
NSDictionary *userInfo = [notification userInfo];
CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect frame = CGRectMake(_chatConversationTableView.frame.origin.x,
_chatConversationTableView.frame.origin.y,
_chatConversationTableView.frame.size.width,
_chatConversationTableView.frame.size.height - size.height);
_chatConversationTableView.frame = frame;
CGRect framee = containerView.frame;
framee.origin.y = self.view.frame.size.height - framee.size.height - size.height;
containerView.frame = framee;
[UIView commitAnimations];
//scroll to bottom of conversation
[self scrollToBottomOfConversation];
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.