Ios 带滚动的UITextView动态设置大小为默认值

Ios 带滚动的UITextView动态设置大小为默认值,ios,objective-c,uitextview,Ios,Objective C,Uitextview,我已经实现了一个ViewController,它的底部UIView包含UITextView,并且禁用了滚动,当您在其中键入时,它会调整大小 当包含的文本高度达到90像素时,我启用滚动-> scrollEnabled = YES; 应该发生什么:UITextView和它的superview应该保持它们被限制的高度(超过90像素的限制) 发生的情况:UITextView将大小调整回其默认值 更多信息: 我使用的代码作为我的底部视图。 我在用iOS7 非常感谢您的帮助,谢谢 编辑:我的代码: - (

我已经实现了一个ViewController,它的底部
UIView
包含
UITextView
,并且禁用了滚动,当您在其中键入时,它会调整大小

当包含的文本高度达到90像素时,我启用滚动->

scrollEnabled = YES;
应该发生什么:UITextView和它的superview应该保持它们被限制的高度(超过90像素的限制)

发生的情况:
UITextView
将大小调整回其默认值

更多信息: 我使用的代码作为我的底部视图。 我在用iOS7

非常感谢您的帮助,谢谢

编辑:我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.textBox.scrollEnabled = NO;
    self.textBox.font = [UIFont fontWithName:@"Helvetica" size:14];
    [self registerForKeyboardNotifications];
}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWasShown:) 
                                                 name:UIKeyboardWillShowNotification 
                                               object:nil];

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

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector: @selector(keyPressed:)
                                                 name: UITextViewTextDidChangeNotification
                                               object: nil];
}

- (void)keyboardWasShown:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    [self setViewMovedUp:YES byHeight:kbSize.height];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    [self setViewMovedUp:NO byHeight:kbSize.height];
}

- (void)keyPressed:(id)sender
{
    CGRect textRect = [self.textBox.text boundingRectWithSize:CGSizeMake(255,MAXFLOAT) 
                                                      options:(NSStringDrawingUsesLineFragmentOrigin) 
                                                   attributes:@{NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:14]} 
                                                      context:nil];
    NSInteger newSizeH = textRect.size.height;
    if (self.textBox.hasText) {
        // if the height of our new chatbox is
        // below 90 we can set the height
        if (newSizeH <= 90) {
            self.textBox.scrollEnabled = NO;
            [self.textBox scrollRectToVisible:CGRectMake(0,0,1,1) 
                                     animated:NO];

            // chatbox
            CGRect chatBoxFrame = self.textBox.frame;
            chatBoxFrame.size.height = newSizeH + 12;
            self.textBox.frame = chatBoxFrame;

            // form view
            CGRect formFrame = self.commentBox.frame;
            formFrame.size.height = 30 + newSizeH;
            self.commentBox.frame = formFrame;
        }

        // if our new height is greater than 90
        // sets not set the height or move things
        // around and enable scrolling
        if (newSizeH > 90) {
            self.textBox.scrollEnabled = YES;
            CGRect frame = self.textBox.frame;
            frame.size.height = 102;
            self.textBox.frame = frame;
            CGRect formFrame = self.commentBox.frame;
            formFrame.size.height = 30 + 90;
            self.commentBox.frame = formFrame;
        }
    }
}

- (void)setViewMovedUp:(BOOL)movedUp byHeight:(CGFloat)height
{
    int movement = movedUp ? -height : height;
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.dataView.frame = CGRectOffset(self.dataView.frame, 0.0, movement);
     }];  
}
-(void)viewDidLoad
{
[超级视图下载];
//从nib加载视图后,执行任何其他设置。
self.textBox.scrollEnabled=否;
self.textBox.font=[UIFont fontWithName:@“Helvetica”大小:14];
[自注册BoardNotifications];
}
-(无效)注册工董事会通知
{
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@选择器(显示键盘:)
名称:UIKeyboardWillShowNotification
对象:无];
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@selector(keyboardWillHide:)
名称:UIKeyboardWillHideNotification
对象:无];
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@选择器(按键:)
名称:UITextViewTextDidChangeNotification
对象:无];
}
-(无效)显示键盘:(NSNotification*)通知
{
NSDictionary*info=[通知用户信息];
CGSize kbSize=[info[UIKeyboardFrameBeginUserInfo]CGRectValue]。大小;
[self-setViewMovedUp:YES byHeight:kbSize.height];
}
-(无效)键盘将隐藏:(NSNotification*)通知
{
NSDictionary*info=[通知用户信息];
CGSize kbSize=[info[UIKeyboardFrameBeginUserInfo]CGRectValue]。大小;
[self-setViewMovedUp:NO byHeight:kbSize.height];
}
-(无效)按键:(id)发送者
{
CGRect textRect=[self.textBox.text boundingRectWithSize:CGSizeMake(255,MAXFLOAT)
选项:(NSStringDrawingUserLineFragmentOrigin)
属性:@{NSFontAttributeName:[UIFont fontWithName:@“Helvetica”大小:14]}
上下文:无];
NSInteger newSizeH=textRect.size.height;
if(self.textBox.hasText){
//如果我们新聊天室的高度是
//低于90我们可以设置高度
如果(newSizeH 90){
self.textBox.scrollEnabled=是;
CGRect frame=self.textBox.frame;
frame.size.height=102;
self.textBox.frame=frame;
CGRect formFrame=self.commentBox.frame;
formFrame.size.height=30+90;
self.commentBox.frame=formFrame;
}
}
}
-(void)setViewMovedUp:(BOOL)movedUp byHeight:(CGFloat)height
{
int movement=movedUp?-高度:高度;
[UIView animateWithDuration:0.3
动画:^{
self.dataView.frame=CGRectOffset(self.dataView.frame,0.0,移动);
}];  
}

以下是我在iOS 6和iOS 7中使用的代码

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {

    // I set _maxTextViewHeight based on device, but you can harcode it to 90
    CGSize size = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, _maxTextViewHeight)];
    float desiredTextViewHeight = size.height - 7.5;
    float desiredInputViewHeight = MIN(desiredTextViewHeight + 16.0f, _maxInputViewHeight);

    textView.scrollEnabled = (desiredInputViewHeight == _maxInputViewHeight);

    NSRange bottom = NSMakeRange([textView.text length] - 1, 1);
    [textView scrollRangeToVisible:bottom];

    CGRect inputViewFrame = _inputView.frame;
    float heightDelta = desiredInputViewHeight - inputViewFrame.size.height;
    inputViewFrame.size.height = desiredInputViewHeight;
    inputViewFrame.origin.y -= heightDelta;

    if (heightDelta != 0) {
        [UIView animateWithDuration:0.1 animations:^{
            _inputView.frame = inputViewFrame;
        } completion:nil];
    }
}

你也可以看看这个项目

虽然我晚了一点,但以防万一。 我自己也面临同样的问题。文本视图恢复为滚动时的原始大小。我解决这个问题的方法是更新UITextView上的高度约束。
也就是说,每次更新UITextView的大小时,都需要更新相应的约束。

滚动不会调整UITextView及其高度的大小。它只能将文本移动到其他位置。您能更清楚地解释一下UITextView如何调整大小吗?UITextView的默认高度是20。根据文本,当我达到90的高度时,我所做的就是将滚动设置为打开,并将UITextView的高度设置为20及其SuperView,因此只需将textView.frame.size.height设置为90即可。为什么不起作用?我把它设为90,甚至更高,它不起作用respond@user2558461你能上传一个有问题的示例项目吗?一般来说,当某些东西不起作用时,最好开始一个新的小项目,并尝试在那里复制。试一试,如果它复制了,请上传,这样我们就可以看一看。谢谢你的回答,我会试试的。请问您是如何根据设备设置_maxInputViewHeight的?我正在写一篇聊天。因此,在
中,keyboard将出现
我计算视图的最大大小:
\u maxInputViewHeight=self.view.bounds.size.height-44.0f-20.0f-keyboard rect.size.height;如果([self.navigationItem.prompt length]>0)\u maxInputViewHeight-=20.0f我正在尝试你的代码,但它仍然会调整大小。如果我删除行
textView.scrollEnabled=(desiredInputViewHeight==\u maxInputViewHeight)它不调整大小,但不限制大小。我尝试创建UIViewController的副本,以查看是否有错误,但似乎没有。你能帮忙吗?但为了让它发挥作用,你需要自己正确计算高度?