如何让UITextField默认键盘显示出来
1个回答
推荐于2016-08-31
展开全部
UIScrollView,
当textViewShouldBeginEditing时候,
用setContentOffset函数
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
NSLog(@" begin editing ");
[postScrollView setContentOffset:CGPointMake(0, 150) animated:YES];
return YES;
}
//写完之后 在某一个事件方法中(如:发送的方法)取消焦点:[postTextView resignFirstResponder]; 接下来会自动运行一下委托:
- (void)textViewDidEndEditing:(UITextView *)textView
{
NSLog(@" did end editing ");
[postScrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
}
第二个问题解答:
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myScrollView addGestureRecognizer: tapGestureRecognizer]; //只需要点击非文字输入区域就会响应hideKeyBoard
- (void)hideKeyBoard:(id)sender
{
[myTextView resignFirstResponder];
//or [myTextField resignFirstResponder];
}
也可以这样做,在UITextField的代理方法textFieldShouldBeginEditiing中,将这个view上移,然后在textFieldShouldEndEditing中将上移的view再下移(动画实现):
上移代码:
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width=self.view.frame.size.width;
float height=self.view.frame.size.height;
CGRect rect=CGRectMake(0.0f,-80,width,height);//上移80个单位,按实际情况设置
self.view.frame=rect;
[UIView commitAnimations];
下移类同,把frame还原即可。
当textViewShouldBeginEditing时候,
用setContentOffset函数
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
NSLog(@" begin editing ");
[postScrollView setContentOffset:CGPointMake(0, 150) animated:YES];
return YES;
}
//写完之后 在某一个事件方法中(如:发送的方法)取消焦点:[postTextView resignFirstResponder]; 接下来会自动运行一下委托:
- (void)textViewDidEndEditing:(UITextView *)textView
{
NSLog(@" did end editing ");
[postScrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
}
第二个问题解答:
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myScrollView addGestureRecognizer: tapGestureRecognizer]; //只需要点击非文字输入区域就会响应hideKeyBoard
- (void)hideKeyBoard:(id)sender
{
[myTextView resignFirstResponder];
//or [myTextField resignFirstResponder];
}
也可以这样做,在UITextField的代理方法textFieldShouldBeginEditiing中,将这个view上移,然后在textFieldShouldEndEditing中将上移的view再下移(动画实现):
上移代码:
NSTimeInterval animationDuration=0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
float width=self.view.frame.size.width;
float height=self.view.frame.size.height;
CGRect rect=CGRectMake(0.0f,-80,width,height);//上移80个单位,按实际情况设置
self.view.frame=rect;
[UIView commitAnimations];
下移类同,把frame还原即可。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询