`
dcj3sjt126com
  • 浏览: 1890402 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ios 弹出键盘挡住UITextView的解决方式

    博客分类:
  • IOS
 
阅读更多

来源: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

分享到:
评论

相关推荐

    ios7隐藏虚拟键盘,解决键盘挡住UITextField问题

    在iOS开发中,特别是在iOS 7及更高版本中,用户界面设计的一个常见问题是虚拟键盘弹出时会遮挡UITextField或UITextView,导致用户无法看到正在输入的文本。本篇文章将详细探讨如何解决这一问题,主要关注如何在iOS 7...

    IOS 解决输入框被键盘遮挡问题

    当用户点击输入框时,系统会自动弹出键盘,但不会自动调整界面布局以适应键盘的高度。这就是导致输入框被遮挡的问题所在。为了解决这个问题,我们可以利用苹果提供的`UIViewController`的一个属性:`...

    iOS项目开发键盘弹出遮挡输入框问题解决方案

    UITextField和UITextView,前者是单行输入文本框,后者是可滑动的多行输入文本框,在这整个开发过程中,我们需要控制键盘的弹出和收起、在输入结束的时候获取输入的信息,此外,我们还需要保证在键盘弹起的时候不...

    swift-iOS平台轻量级的键盘管理器使用简单功能强大键盘再也不会挡住输入控件

    "swift-iOS平台轻量级的键盘管理器使用简单功能强大键盘再也不会挡住输入控件" 这个标题所提及的,就是解决了一个常见的问题:当用户在文本框(UITextField或UITextView)中输入时,弹出的键盘可能会遮挡到输入控件...

    ios-键盘遮挡输入框处理.zip

    在iOS中,当用户点击输入框时,系统会自动弹出键盘以便用户输入。由于iPhone屏幕尺寸有限,特别是在一些底部有输入框的视图中,键盘弹出后很容易将输入框覆盖。这个问题在滚动视图(UIScrollView、UITableView或...

    键盘挡住问题

    在iOS开发中,当用户在UITextField或UITextView中输入时,弹出的键盘经常会遮挡这些文本输入框,导致用户无法直接看到正在编辑的内容。这个问题被称为“键盘挡住问题”,是移动应用开发中常见的用户体验问题。在给定...

    iphone 解决键盘遮挡和view传值的一个小例子

    在iOS开发中,我们经常会遇到两个常见的问题:一是键盘弹出时遮挡了屏幕下方的输入框,导致用户无法看到正在输入的内容;二是不同View之间的数据传递,这在多个界面间进行信息交互时尤为重要。本篇文章将通过一个...

    键盘和输入框

    为了不让键盘挡住输入框,我们可以使用`UIScrollView`或`UITableView`作为输入框的容器。当键盘弹出时,可以调整滚动视图的内容偏移量,使输入框保持在可视区域内。具体做法如下: 1. **监听键盘通知**:注册键盘...

Global site tag (gtag.js) - Google Analytics