當鍵盤被隱藏時,滾動視圖應該回到它的原始contentInset,但它在iOS7中不起作用。設置顯示鍵盤時的scrollview的contentInset正在工作,但當鍵盤被隱藏時,scrollview的contentInset無法設置爲插入零。 代碼:iOS7 UIScrollView contentInset不起作用
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:Nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)keyboardWasShown:(NSNotification *)notif
{
CGSize keyboardSize = [[[notif userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0);
UIScrollView *scrollView = (UIScrollView *)self.view;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
CGRect rect = self.view.frame;
rect.size.height -= keyboardSize.height;
if (!CGRectContainsPoint(rect, self.wishContentField.frame.origin)) {
CGPoint point = CGPointMake(0, self.wishContentField.frame.origin.y - keyboardSize.height);
[scrollView setContentOffset:point animated:YES];
}
}
- (void)keyboardWasHidden:(NSNotification *)notif
{
UIEdgeInsets zeroInsets = UIEdgeInsetsZero;
UIScrollView *scrollView = (UIScrollView *)self.view;
[scrollView setContentInset:zeroInsets];
scrollView.scrollIndicatorInsets = zeroInsets;
}
你能解釋一下它不工作(如何在鍵盤隱藏後行事)?請注意,在iOS 7上,如果您有半透明的'navigationBar',則您的視圖控制器將爲您的scrollView設置頂部插入,否則將不會設置。這可能就是這種情況,因爲你正在設置'contentInset.top = 0',所以它可能隱藏'navigationBar'或'statusBar'後面的一些內容。 –
感謝您的回覆。我將頂部設置爲navigationBar.frame.size.height,現在它正在工作。 –
寫一個答案,並給alex-i一些功勞;) –