4 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊
實(shí)際上,最好只使用Apple的實(shí)現(xiàn),如文檔中所提供的那樣。但是,他們提供的代碼是錯(cuò)誤的。將keyboardWasShown:
評(píng)論下方的部分替換為以下內(nèi)容:
NSDictionary* info = [aNotification userInfo];
CGRect keyPadFrame=[[UIApplication sharedApplication].keyWindow convertRect:[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] fromView:self.view];
CGSize kbSize =keyPadFrame.size;
CGRect activeRect=[self.view convertRect:activeField.frame fromView:activeField.superview];
CGRect aRect = self.view.bounds;
aRect.size.height -= (kbSize.height);
CGPoint origin = activeRect.origin;
origin.y -= backScrollView.contentOffset.y;
if (!CGRectContainsPoint(aRect, origin)) {
CGPoint scrollPoint = CGPointMake(0.0,CGRectGetMaxY(activeRect)-(aRect.size.height));
[backScrollView setContentOffset:scrollPoint animated:YES];
}
Apple代碼的問(wèn)題是這些:(1)它們總是計(jì)算點(diǎn)是否在視圖的框架內(nèi),但它是a ScrollView,所以它可能已經(jīng)滾動(dòng),你需要考慮該偏移:
origin.y -= scrollView.contentOffset.y
(2)他們將contentOffset移動(dòng)鍵盤(pán)的高度,但我們想要相反(我們想要移動(dòng)contentOffset屏幕上可見(jiàn)的高度,而不是不是):
activeField.frame.origin.y-(aRect.size.height)

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊
在textFieldDidBeginEditting
和textFieldDidEndEditing
通話功能[self animateTextField:textField up:YES]
,如下所示:
-(void)textFieldDidBeginEditing:(UITextField *)textField { [self animateTextField:textField up:YES]; }- (void)textFieldDidEndEditing:(UITextField *)textField{ [self animateTextField:textField up:NO];}-(void)animateTextField:(UITextField*)textField up:(BOOL)up{ const int movementDistance = -130; // tweak as needed const float movementDuration = 0.3f; // tweak as needed int movement = (up ? movementDistance : -movementDistance); [UIView beginAnimations: @"animateTextField" context: nil]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationDuration: movementDuration]; self.view.frame = CGRectOffset(self.view.frame, 0, movement); [UIView commitAnimations];}
我希望這段代碼可以幫到你。
在Swift 2中
func animateTextField(textField: UITextField, up: Bool) { let movementDistance:CGFloat = -130 let movementDuration: Double = 0.3 var movement:CGFloat = 0 if up { movement = movementDistance } else { movement = -movementDistance } UIView.beginAnimations("animateTextField", context: nil) UIView.setAnimationBeginsFromCurrentState(true) UIView.setAnimationDuration(movementDuration) self.view.frame = CGRectOffset(self.view.frame, 0, movement) UIView.commitAnimations()}func textFieldDidBeginEditing(textField: UITextField) { self.animateTextField(textField, up:true)}func textFieldDidEndEditing(textField: UITextField) { self.animateTextField(textField, up:false)}
SWIFT 3
func animateTextField(textField: UITextField, up: Bool) { let movementDistance:CGFloat = -130 let movementDuration: Double = 0.3 var movement:CGFloat = 0 if up { movement = movementDistance } else { movement = -movementDistance } UIView.beginAnimations("animateTextField", context: nil) UIView.setAnimationBeginsFromCurrentState(true) UIView.setAnimationDuration(movementDuration) self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement) UIView.commitAnimations() } func textFieldDidBeginEditing(textField: UITextField) { self.animateTextField(textField: textField, up:true) } func textFieldDidEndEditing(textField: UITextField) { self.animateTextField(textField: textField, up:false) }
- 4 回答
- 0 關(guān)注
- 711 瀏覽
添加回答
舉報(bào)