来源:http://2015.iteye.com/blog/1325291
有一个320*480的UITextView,点击UITextView的时候,下面的部分会被弹出的软键盘挡住,我们可以将UITextView的高度改为480 - 软键盘的高度,关闭软键盘后,高度恢复为原始高度。
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView = [[UITextView alloc] initWithFrame:self.view.frame];
self.textView.textColor = [UIColor blackColor];
self.textView.font = [UIFont fontWithName:@"Arial" size:18];
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.text = @"This is the text view example, we can edit, delete, add content in the text view.";
self.textView.returnKeyType = UIReturnKeyDefault;
self.textView.keyboardType = UIKeyboardTypeDefault;
self.textView.scrollEnabled = YES;
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview: self.textView];
[self.textView release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.textView = nil;
}
- (void)dealloc {
[textView release], textView = nil;
[super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height -= keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.size.height += keyboardRect.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
也可以参考帖子:http://www.cnblogs.com/iphone520/archive/2011/10/11/2207616.html
相关推荐
在iOS开发中,特别是在iOS 7及更高版本中,用户界面设计的一个常见问题是虚拟键盘弹出时会遮挡UITextField或UITextView,导致用户无法看到正在输入的文本。本篇文章将详细探讨如何解决这一问题,主要关注如何在iOS 7...
当用户点击输入框时,系统会自动弹出键盘,但不会自动调整界面布局以适应键盘的高度。这就是导致输入框被遮挡的问题所在。为了解决这个问题,我们可以利用苹果提供的`UIViewController`的一个属性:`...
UITextField和UITextView,前者是单行输入文本框,后者是可滑动的多行输入文本框,在这整个开发过程中,我们需要控制键盘的弹出和收起、在输入结束的时候获取输入的信息,此外,我们还需要保证在键盘弹起的时候不...
"swift-iOS平台轻量级的键盘管理器使用简单功能强大键盘再也不会挡住输入控件" 这个标题所提及的,就是解决了一个常见的问题:当用户在文本框(UITextField或UITextView)中输入时,弹出的键盘可能会遮挡到输入控件...
在iOS中,当用户点击输入框时,系统会自动弹出键盘以便用户输入。由于iPhone屏幕尺寸有限,特别是在一些底部有输入框的视图中,键盘弹出后很容易将输入框覆盖。这个问题在滚动视图(UIScrollView、UITableView或...
在iOS开发中,当用户在UITextField或UITextView中输入时,弹出的键盘经常会遮挡这些文本输入框,导致用户无法直接看到正在编辑的内容。这个问题被称为“键盘挡住问题”,是移动应用开发中常见的用户体验问题。在给定...
在iOS开发中,我们经常会遇到两个常见的问题:一是键盘弹出时遮挡了屏幕下方的输入框,导致用户无法看到正在输入的内容;二是不同View之间的数据传递,这在多个界面间进行信息交互时尤为重要。本篇文章将通过一个...
为了不让键盘挡住输入框,我们可以使用`UIScrollView`或`UITableView`作为输入框的容器。当键盘弹出时,可以调整滚动视图的内容偏移量,使输入框保持在可视区域内。具体做法如下: 1. **监听键盘通知**:注册键盘...