来源: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
相关推荐
UITextField或者UITextView放在UIScrollView上弹出键盘修改UIScrollView上移。 还是有点小小的不完美,并不影响整体使用
AnimatedTextInput, iOS的动画UITextField和UITextView替换 AnimatedTextInput iOS Jobandtalent应用中使用的iOS自定义文本输入组件。 安装使用cocoapods在项目中安装这里自定义控件。pod 'AnimatedTextInput'
UITextField和UITextView,前者是单行输入文本框,后者是可滑动的多行输入文本框,在这整个开发过程中,我们需要控制键盘的弹出和收起、在输入结束的时候获取输入的信息,此外,我们还需要保证在键盘弹起的时候不...
WPAutoSpringKeyboard 一个为编辑区域自动弹出键盘的ViewController类
自适应UITextView和键盘位置对应的Demo,解决在UITextView输入时被键盘挡住的问题。
通过注册两个通知,可以监控键盘的弹出和消失,可以动态计算出键盘的高度。
第一步导入头文件 第二步设置属性 就这么简单!
本程序演示了在点击UITextView弹出键盘后,添加一个遮罩层将键盘一起遮住的效果。
简单的基于NSMutableAttributedString实现的图文混排,表情也可以用这种方式实现,做个参考而已
主要介绍了IOS 开发UITextView回收或关闭键盘的相关资料,需要的朋友可以参考下
在iOS开发中,UITextFiled和UITextView是很常见的两个控件,当我们设置好这两个控件后,点击文字输入区域,系统会自动弹出键盘,但是如何收起键盘、点击哪里收起键盘,以及在iPhone4中键盘弹出后遮挡输入框怎么办...
UITextView输入文字过多时候,键盘会将TextView遮挡,本Demo是通过计算文字高度从而改变TextView的文字显示位置。
直接将封装好的 放到你要用的工程中调用即可,可在根据自己的需要自行扩展,基本的功能都已经实现了,超级简单,通俗易懂的、
UITextView 实现UITextField功能
UITextView操作,代码细化
UITableViewCell 动态高度计算 适用于ios8.0后 支持UITextView输入动态改变高度。
继承自UITextView,且带有占位符和block回调的自定义textview SYPlaceHolderTextView:https://github.com/potato512/SYPlaceHolderTextView
ios源代码DTCoreText-1.6.10在UITextView上实现十分丰富的文字效果
iOS 原生UITextView 富文本编辑器.zip
IOS 在UITextView上实现十分丰富的文字效果