arm*_*man 6 iphone objective-c uiscrollview uikeyboard ios
我一直在阅读Apple提供的文本编程指南,通过以下网址:
我试图阻止按钮和textFields被键盘重叠.
当我按照指南的说明粘贴清单5-1(下面列出)中的代码时,我收到的错误如下:
" Use of unidentified identifier 'scrollView'; Did you mean 'UIScrollView' ? "
Run Code Online (Sandbox Code Playgroud)
和
" 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;
}
Run Code Online (Sandbox Code Playgroud)
尝试此代码将解决您的问题,替换TextView为TextField
-(void)textViewDidBeginEditing:(UITextView *)textView{
[self animateTextFild:textView up:YES];
}
-(void)textViewDidEndEditing:(UITextView *)textView{
[self animateTextFild:textView up:NO];
[textView resignFirstResponder];
}
-(void) animateTextFild:(UITextView *)textField up:(BOOL)up{
int animatedDistance;
int moveUpValue = textField.frame.origin.y+ textField.frame.size.height;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = 216-(730-moveUpValue);
}
else
{
animatedDistance = 162-(520-moveUpValue);
}
if(animatedDistance>0)
{
const int movementDistance = animatedDistance;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
myView.frame = CGRectOffset(ViewEdit.frame, 0, movement);
[UIView commitAnimations];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1323 次 |
| 最近记录: |