如果鍵盤將隱藏UITextField,我正在滾動UIScrollView的過程中。如果它會被掩蓋,我會減少UIScrollView的高度,這很好。但是當我試圖增長UIScrollView的高度(回到原始大小)時,整個UIScrollView向上移動然後動畫到原始大小和位置。原點向上移動X量並向下移動到應該在的位置,而不是向下擴展視圖的高度。使用動畫塊調整UIScrollView的大小移動原點
- (void)keyboardWillShow:(NSNotification *)n
{
keyboardMove = self.rightScrollView.frame;
offsetMove = self.rightScrollView.contentOffset;
NSDictionary* userInfo = [n userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:.25
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^
{
self.rightScrollView.frame = CGRectMake(self.rightScrollView.frame.origin.x, self.rightScrollView.frame.origin.y, self.rightScrollView.frame.size.width, (self.view.frame.size.height - (self.rightScrollView.frame.origin.y + keyboardSize.height)));
}
completion:nil];
if ((activeTextField.frame.origin.y + activeTextField.frame.size.height) > self.rightScrollView.frame.size.height)
{
float contentOffsetMove = (self.rightScrollView.contentOffset.y + (activeTextField.frame.origin.y - self.rightScrollView.frame.size.height) + activeTextField.frame.size.height + 10);
self.rightScrollView.contentOffset = CGPointMake(self.rightScrollView.contentOffset.x, contentOffsetMove);
}
}
- (void)keyboardWillHide:(NSNotification *)n
{
if ((activeTextField.frame.origin.y + activeTextField.frame.size.height) > self.rightScrollView.frame.size.height)
{
[UIView animateWithDuration:.25
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[self.rightScrollView setContentOffset:offsetMove animated:YES];
self.rightScrollView.frame = keyboardMove;
}
completion:nil];
}
else
{
[UIView animateWithDuration:.25
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
self.rightScrollView.frame = keyboardMove;
}
completion:nil];
}
}
所以,當鍵盤隱藏,self.rightScrollView.origin向上移動X點,然後動畫的位置是應該的。我需要保持原點,高度變得更高(向下增長)。
任何想法爲什麼它的行爲愚蠢?
你設法解決這個問題?我有完全相同的問題,找不到解決方案嗎? – pnizzle 2012-09-25 03:25:15