我在iphone-5s的设备全屏设置了UITextView,textView大小为(0,44,320,524)。当键盘出现时,用户无法在view中显示插入文本。我可以管理UITextView,我可以使用UIScrollView或其他任何东西吗?
答案 0 :(得分:2)
编辑完整视图时会向上移动,完成编辑后会向下移动......
(void)textViewDidBeginEditing:(UITextView *)textView {
[self animateTextView: YES];
}
(void)textViewDidEndEditing:(UITextView *)textView {
[self animateTextView:NO];
}
(void) animateTextView:(BOOL) up {
const int movementDistance =heightKeyboard; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement= movement = (up ? -movementDistance : movementDistance);
NSLog(@"%d",movement);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
[UIView commitAnimations];
}
我希望它对你有用。
答案 1 :(得分:1)
我已使用此代码
解决了这个问题.h文件定义一个值
CGRect originalTextViewFrame;
.m文件
- (void)viewWillAppear:(BOOL)animated
{
// Register notifications for when the keyboard appears
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification*)notification
{
[self moveTextViewForKeyboard:notification up:YES];
}
- (void)keyboardWillHide:(NSNotification*)notification
{
[self moveTextViewForKeyboard:notification up:NO];
}
- (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up
{
NSDictionary *userInfo = [notification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardRect;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
if (up == YES) {
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = textView.frame;
originalTextViewFrame = textView.frame;
newTextViewFrame.size.height = keyboardTop - textView.frame.origin.y - 10;
textView.frame = newTextViewFrame;
}
else
{
// Keyboard is going away (down) - restore original frame
textView.frame = originalTextViewFrame;
}
[UIView commitAnimations];
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
// Any new character added is passed in as the "text" parameter
if ([text isEqualToString:@"\n"])
{
// Be sure to test for equality using the "isEqualToString" message
[textView resignFirstResponder];
//[scrlView setContentOffset:CGPointMake(0,0) animated:YES];
// Return FALSE so that the final '\n' character doesn't get added
return FALSE;
}
// For any other character return TRUE so that the text gets added to the view
return TRUE;
}
答案 2 :(得分:0)
使用滚动视图,然后尝试
UITextViewDelegate
方法
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[scrollView setContentOffset:CGPointMake(0,yourtextview.center.y-200) animated:YES];
}