Iphone 有键盘时如何使UITextView上移
如何在开始输入某个值时上下移动UITextView。就像在TextField中一样,我们使用它的委托方法。如果出现UITextView,该怎么办?Iphone 有键盘时如何使UITextView上移,iphone,ios,objective-c,uitextfield,uitextview,Iphone,Ios,Objective C,Uitextfield,Uitextview,如何在开始输入某个值时上下移动UITextView。就像在TextField中一样,我们使用它的委托方法。如果出现UITextView,该怎么办?#为键盘80.0定义kOFFSET_ #define kOFFSET_FOR_KEYBOARD 80.0 -(void)keyboardWillShow { // Animate the current view out of the way if (self.view.frame.origin.y >= 0) {
#为键盘80.0定义kOFFSET_
#define kOFFSET_FOR_KEYBOARD 80.0
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:mailTf])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[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
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
-(无效)键盘将显示{
//设置当前视图的动画,使其不碍事
if(self.view.frame.origin.y>=0)
{
[self-setViewMovedUp:是];
}
else if(self.view.frame.origin.y<0)
{
[self-setViewMovedUp:否];
}
}
-(无效)键盘将隐藏{
if(self.view.frame.origin.y>=0)
{
[self-setViewMovedUp:是];
}
else if(self.view.frame.origin.y<0)
{
[self-setViewMovedUp:否];
}
}
-(无效)textFieldDidBeginEditing:(UITextField*)发送方
{
如果([发送方isEqual:mailTf])
{
//移动主视图,使键盘不会隐藏它。
if(self.view.frame.origin.y>=0)
{
[self-setViewMovedUp:是];
}
}
}
//方法在显示/关闭键盘时向上/向下移动视图
-(无效)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil上下文:NULL];
[UIView setAnimationDuration:0.3];//如果要向上滑动视图
CGRect rect=self.view.frame;
if(movedUp)
{
//1.向上移动视图的原点,以便隐藏的文本字段位于键盘上方
//2.增大视图的大小,以便覆盖键盘后面的区域。
rect.origin.y-=kOFFSET\u用于键盘;
rect.size.height+=键盘的kOFFSET\u;
}
其他的
{
//恢复到正常状态。
rect.origin.y+=kOFFSET_用于键盘;
rect.size.height-=kOFFSET\u用于键盘;
}
self.view.frame=rect;
[UIView委员会];
}
-(无效)视图将显示:(BOOL)动画
{
//注册键盘通知
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@选择器(键盘将显示)
名称:UIKeyboardWillShowNotification
对象:无];
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@选择器(键盘将隐藏)
名称:UIKeyboardWillHideNotification
对象:无];
}
-(无效)视图将消失:(BOOL)已设置动画
{
//在不可见时注销键盘通知。
[[NSNotificationCenter defaultCenter]移除观察者:self
名称:UIKeyboardWillShowNotification
对象:无];
[[NSNotificationCenter defaultCenter]移除观察者:self
名称:UIKeyboardWillHideNotification
对象:无];
}
尝试此代码….我通过更改帧来执行此操作
-(void)textViewDidBeginEditing:(UITextView *)textView{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES];
viewMsg.frame = CGRectMake(10, 50, 300, 200);
[UIView commitAnimations];
NSLog(@"Started editing target!");
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES];
viewMsg.frame = CGRectMake(10, 150, 300, 200);
[UIView commitAnimations];
}
下面是一个示例代码,它将为您自动处理键盘
如果使用TableView,则TableView必须是
TPKeyboardAvoidingTableView
的子类;如果使用scrollview,则scrollview必须是TPKeyboardAvoidingScrollView
的子类。此库将自动为您执行键盘处理。编辑时,完整视图将向上移动,编辑完成后将向下移动
- (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];
}
我希望这能帮助你…我建议你看看 特别是在第节:移动位于键盘下的内容。
我已经多次成功地使用了这种方法。
textfield变为活动的
>相应地移动文本字段。请看这个链接是不是很难使用搜索?即使是这样,也要看侧边栏:它显示了相关的问题。同样的问题也有。annnndd你应该使用块来制作动画,你的方法是不可取的,因为iOS4…这是textField的答案,而不是textView…并且你不应该使用常量作为键盘大小,我们鼓励您从Apple获取偏移量大小。嗯,您应该使用块来制作动画,您的方法不受欢迎,因为iOS4I在这段代码中遇到了很多问题。通过将“self.inputView.frame”替换为“self.view.frame”解决了此问题。当您无法使用scrollview解决方案时,这是获得键盘高度的最简单和最好的方法?谢谢