当键盘以自动布局弹出时调整UITextView的大小 [英] Resizing an UITextView when the keyboard pops up with auto layout

查看:77
本文介绍了当键盘以自动布局弹出时调整UITextView的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在键盘显示时调整UITextView的大小,但是我似乎无法做到这一点.我创建了一个带有UITextView的视图.在代码中,我想手动调整此文本视图的高度.我这样做:

I would like to resize a UITextView when the keyboard shows up but I can't seem to do this. I've created a single view with an UITextView in it. In code, I would like to manually resize the height of this text view. I do this:

_textView.translatesAutoresizingMaskIntoConstraints  = NO;

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:_textView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:216.0f];
[_textView addConstraint:constraint];

在Interface Builder中,我说文本视图和超级视图之间的边距可以大于或等于0.

In Interface Builder, I say that the margin between the text view and the superview can be greater than or equal to 0.

当我运行该应用程序时,它给出了无法同时满足约束条件的错误:

When I run the app, it gives the error that constrains can't be satisfied simultanously:

"<NSLayoutConstraint:0x886e550 V:[UITextView:0x7b0fa00(216)]>",
"<NSLayoutConstraint:0x886f7c0 UITextView:0x7b0fa00.bottom == UIView:0x886e3c0.bottom>",
"<NSLayoutConstraint:0x886f700 V:|-(0)-[UITextView:0x7b0fa00]   (Names: '|':UIView:0x886e3c0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x71a2d10 h=--- v=--- V:[UIWindow:0x8a792f0(480)]>",
"<NSAutoresizingMaskLayoutConstraint:0x71a1490 h=-&- v=-&- UIView:0x886e3c0.height == UIWindow:0x8a792f0.height - 20>"


Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x886e550 V:[UITextView:0x7b0fa00(216)]>

我不确定如何确保满足所有约束条件.有人可以帮我吗?

I'm not sure on how to make sure that all constraints are satisfied. Could anyone give me a hand in this?

谢谢!

推荐答案

可能的副本

如何在使用键盘时在iOS上调整UITextView的大小出现吗?吗?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification object:self.view.window]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                             name:UIKeyboardWillHideNotification object:self.view.window]; 

- (void)keyboardWillShow:(NSNotification *)notif
{
[thetextView setFrame:CGRectMake(20, 49, 280, 187)]; //Or where ever you want the view to go


}

- (void)keyboardWillHide:(NSNotification *)notif
{
[thetextView setFrame:CGRectMake(20, 49, 280, 324)]; //return it to its original position

}

这篇关于当键盘以自动布局弹出时调整UITextView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆