我已經把它放在我的脖子上,試圖讓用戶在各個對象上看起來無縫的時候將整個視圖移動到合適的UITextField
。我知道我並不是唯一一個絕對不喜歡這樣做的人。鍵盤出現時滾動視圖:庫或DIY?
什麼是最好的方法使盡可能少的工作儘可能美麗的工作?我試過TPKeyboardAvoiding,它完全吸了。
現在,我已經有了這個代碼去,但它在它自己的特殊方式吸收以及:
- (void)viewDidLoad
{
self.view.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin);
self.scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
- (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);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.activeField.frame.origin)) {
CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y-kbSize.height);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
你願意分享特別的方式嗎? – borrrden 2013-02-20 03:32:57
當你最初點擊一個文本框(有很多)時,它很好用,但是當你點擊鍵盤上的返回按鈕時,它不會滾動到下一個「UITextField」。 – jakenberg 2013-02-20 03:37:12
擊中返回從不滾動到下一個文本框。只有在移動Safari中激活表單時纔會發生這種情況。你必須爲此實現你自己的按鈕。編輯:這甚至沒有發生在瀏覽器中。你在鍵盤上得到了一個特殊的工具欄來做到這一點... – Ron 2013-02-20 03:46:56