我一直在讀通過以下網址由蘋果公司提供的文本編程指南:防止鍵盤重疊的UIButton /的UITextField
我試圖阻止按鈕和文本框從鍵盤重疊。
當我粘貼從列表5-1中的代碼(下面列出)按照導的指令I接收錯誤,如:
" Use of unidentified identifier 'scrollView'; Did you mean 'UIScrollView' ? "
和
" Property 'contentsInset' not found in 'scrollView' "
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// 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)) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
你實際使用滾動型? – GenieWanted
是的我已經在scrollView中嵌入了UIViewController的內容(兩個textFields和一個按鈕)@GenieWanted – arman
你的UIScrollView的名稱是什麼?您需要指定該位置來代替scrollView。 – GenieWanted