2013-12-17 71 views
4

,我实现了具有包含UITextView禁用滚动您键入内部的重新调整底部UIView一个视图控制器。的UITextView与滚动动态调整大小设置为默认

当文本的高度载达到90个像素,启用滚动 - >

scrollEnabled = YES; 

什么是应该发生的:UITextView及其superview应该保持原样,他们仅限于高度(超过90像素限制)。

发生了什么:UITextView调整回它的默认值。

更多信息: 我使用的Multiline UITextField代码为我仰视图。 我正在使用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); 
    }]; 
} 
+0

滚动不会调整UITextView的大小及其高度。它只能将文本移动到另一个地方。你能更清楚地解释UITextView如何调整大小吗? – Miroslav

+0

的UITextView的默认高度为20,根据文本时我到达90的高度,我要做的就是设置滚动并将其设置的UITextView 20的高度,以及它的父 – user2558461

+0

所以只设置textView.frame .size.height = 90.它为什么不起作用? – Miroslav

回答

0

这里是你可以看看在这个项目中的代码,我在iOS 6中使用和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]; 
    } 
} 

而且https://github.com/jessesquires/MessagesTableViewController

+0

谢谢您的回答,我会尝试一下。我可能请问你是怎么设置的_maxInputViewHeight根据设备 – user2558461

+0

我写的一个聊天所以在'keyboardWillAppear'我计算的看法最大尺寸:?'_maxInputViewHeight = self.view.bounds.size.height - 44.0f - 20.0f - keyboardRect.size.height; 如果([self.navigationItem.prompt长度]> 0)_maxInputViewHeight - = 20.0f;' –

+0

我试着你的代码,但它仍然调整大小。如果我删除行'textView.scrollEnabled =(desiredInputViewHeight == _maxInputViewHeight);'它不会调整大小,但不会限制大小。我试图创建一个UIViewController的副本来查看我是否犯了错误,但看起来并不像它。你能帮忙吗? – user2558461

1

虽然我有点晚了,但只是开始。 我自己也面临同样的问题。文本视图在滚动时回到原始大小。我解决它的方式是更新UITextView上的高度约束。 即每次更新UITextView的大小时,还需要更新相应的约束条件。