当键盘存在时,UIView编程约束高度增加

时间:2015-01-01 06:10:19

标签: ios objective-c uiview keyboard

我正在为应用构建评论输入控件。此控件包含UITextView中嵌入的UIView。所有约束都以编程方式处理。当用户点击UITextView时,键盘将会打开,会发生什么。这将调用键盘观察器方法,然后我调整注释输入控件的底部约束以使用键盘向上移动。但是,我也在尝试同时增加输入控件的高度,以便用户有更多的输入空间。我无法实现这一目标。

-(void)updateViewConstraints
{

  NSDictionary *views = @{
                          @"table"           : self.commentsTableView,
                          @"seeMoreComments" : self.seeMoreCommentsView,
                          @"commentInput"    : self.commentEntryInput
                          };

  //See More Comments Constraints
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[seeMoreComments]-0-|" options:0 metrics:nil views:views]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[seeMoreComments(45)]" options:0 metrics:nil views:views]];

  //Table view constraints
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[table]-0-|" options:0 metrics:nil views:views]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[seeMoreComments]-0-[table]-0-|" options:0 metrics:nil views:views]];

  //Comment entry input
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[commentInput]-0-|" options:0 metrics:nil views:views]];

  commentInputVerticalConstraint = [NSLayoutConstraint constraintWithItem:self.commentEntryInput
                                                                attribute:NSLayoutAttributeHeight
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:nil
                                                                attribute:NSLayoutAttributeHeight
                                                               multiplier:1.0
                                                                 constant:commentInputHeight];

  if(commentInputBottomConstraint == nil)
  {
    commentInputBottomConstraint =
    [NSLayoutConstraint constraintWithItem:self.commentEntryInput
                                 attribute:NSLayoutAttributeBottom
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.view
                                 attribute:NSLayoutAttributeBottom multiplier:1.0
                                  constant:0.0];

  }

  [self.view addConstraint:commentInputVerticalConstraint];
  [self.view addConstraint:commentInputBottomConstraint];

  [super updateViewConstraints];
}

现在我有一个在调用keyBoardWillShow时调用的方法。当键盘出现时,此方法会动画注释输入控件。

    (void)animateContentWithKeyboardInfo:(NSDictionary *)keyboardInfo
    {
      NSNumber                *animationDuration  = keyboardInfo[ UIKeyboardAnimationDurationUserInfoKey ];
      NSValue                 *keyboardFrameValue = keyboardInfo[ UIKeyboardFrameEndUserInfoKey ];
      CGRect                  keyboardFrame       = [keyboardFrameValue CGRectValue];
      UIViewAnimationCurve    animationCurve      = [keyboardInfo[ UIKeyboardAnimationCurveUserInfoKey ] intValue];

      UIViewAnimationOptions  animationOptions    = animationOptionWithCurve(animationCurve);

      commentInputBottomConstraint.constant = (keyboardFrame.origin.y - [UIScreen mainScreen].bounds.size.height);

      //Increase the veritcal height of the comment input control
      commentInputVerticalConstraint.constant = 125;

      //Takes into account that the Tab Bar is 50 points, and adjust for this
      //value.

      if(keyboardAppeared == YES)
      {
        commentInputBottomConstraint.constant += TAB_BAR_OFFSET;
      }

      else
      {
        commentInputBottomConstraint.constant -= TAB_BAR_OFFSET;
      }

      [self.view layoutIfNeeded];

      [self.view setNeedsUpdateConstraints];

      [UIView animateWithDuration:[animationDuration floatValue] delay:0.0 options:animationOptions animations:
       ^{


        [self.view layoutIfNeeded];

       } completion:nil];
    }

然而,当我尝试调整commentInputVerticalConstraint的常量时,我​​收到此错误消息:

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the `UIView` property `translatesAutoresizingMaskIntoConstraints`) 
(
    "<NSLayoutConstraint:0x1899fcb0 V:[CommentEntryInput:0x176e5160(50)]>",
    "<NSLayoutConstraint:0x1899e5c0 V:[CommentEntryInput:0x176e5160(125)]>"
)

我不确定我是否有办法重置&#34;或者调整约束以在键盘出现时进行处理,然后在键盘消失时将其恢复正常。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的问题是-(void)updateViewConstraints被多次调用。因此,您要创建一个新约束并将其添加到视图两次。尝试检查约束是否为零。

在动画更改常量之前,我也认为您不需要第一个[self.view layoutIfNeeded]。更改常量时,只需设置它然后将[self.view layoutIfNeeded]包装在动画块中以设置为该新值的动画。