【问题标题】:Change height of inputAccessoryView issue更改 inputAccessoryView 问题的高度
【发布时间】:2015-02-09 18:07:35
【问题描述】:

当我在 iOS 8 中更改inputAccessoryView 的高度时,inputAccessoryView 没有转到正确的原点,而是覆盖了键盘。

这里有一些代码sn-ps:

在表格视图控制器中

- (UIView *)inputAccessoryView {
    if (!_commentInputView) {
        _commentInputView = [[CommentInputView alloc] initWithFrame:CGRectMake(0, 0, [self width], 41)];
        [_commentInputView setPlaceholder:NSLocalizedString(@"Comment", nil) andButtonTitle:NSLocalizedString(@"Send", nil)];
        [_commentInputView setBackgroundColor:[UIColor whiteColor]];
        _commentInputView.hidden = YES;
        _commentInputView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
    }

    return _commentInputView;
}

在评论输入视图中

#when the textview change height
- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {
    if (height > _textView_height) {
        [self setHeight:(CGRectGetHeight(self.frame) + height - _textView_height)];
        [self reloadInputViews];
    }
}

在 UIView 类别中来自ios-helpers

- (void)setHeight: (CGFloat)heigth {
    CGRect frame = self.frame;
    frame.size.height = heigth;
    self.frame = frame;
}

【问题讨论】:

  • 请贴一些代码

标签: ios ios8 inputaccessoryview


【解决方案1】:

对于 Xcode 11.2 和 swift 5,即使在动画块中,此函数也会更新 inputAccessoryView 约束

func updateInputContainerConstraints() {
    if let accessoryView = inputAccessoryView,
        let constraint = accessoryView.superview?.constraints.first(where: { $0.identifier == "accessoryHeight" }) {
        constraint.isActive = false
        accessoryView.layoutIfNeeded()
        constraint.constant = accessoryView.bounds.height
        constraint.isActive = true
        accessoryView.superview?.addConstraint(constraint)
        accessoryView.superview?.superview?.layoutIfNeeded()
    }
}

【讨论】:

    【解决方案2】:

    试试这个: _vwForSendChat 是输入附件视图 _txtViewChatMessage 是输入附件视图中的文本视图

    -(void)textViewDidChange:(UITextView *)textView {
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    if (newFrame.size.height < 40) {
        _vwForSendChat.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
    } else {
        if (newFrame.size.height > 200) {
            _vwForSendChat.frame = CGRectMake(0, 0, self.view.frame.size.width, 200);
        } else {
           _vwForSendChat.frame = CGRectMake(0, 0, self.view.frame.size.width, newFrame.size.height);
        }
    }
    [self.txtViewChatMessage reloadInputViews];
    }
    

    【讨论】:

      【解决方案3】:

      在更改 inputAccessoryView 的高度时,您可以更新 Yijun 的回答中提到的约束的一种方法是在您的 inputAccessoryView 上覆盖 setFrame:。这不依赖于高度约束是数组中的第一个。

      - (void)setFrame:(CGRect)frame {
          [super setFrame:frame];
      
          for (NSLayoutConstraint *constraint in self.constraints) {
              if (constraint.firstAttribute == NSLayoutAttributeHeight) {
                  constraint.constant = frame.size.height;
                  break;
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        终于,我找到了答案。在ios8中,苹果在inputAccessoryView中添加了一个NSContentSizeLayoutConstraints,并设置了一个44的常量。这个常量不能去掉,因为ios8用它来计算inputAccessoryView的高度。所以,唯一的解决办法就是改变这个常量的值。

        示例

        在 ViewDidAppear 中

        - (void)viewDidAppear:(BOOL)animated {
            if ([self.inputAccessoryView constraints].count > 0) {
                NSLayoutConstraint *constraint = [[self.inputAccessoryView constraints] objectAtIndex:0];
                constraint.constant = CommentInputViewBeginHeight;
            }
        }
        

        当textview高度改变时改变inputAccessoryView高度

        - (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {
        
            NSLayoutConstraint *constraint = [[self constraints] objectAtIndex:0];
            float new_height = height + _textView_vertical_gap*2;
        
            [UIView animateWithDuration:0.2 animations:^{
                constraint.constant = new_height;
            } completion:^(BOOL finished) {
                [self setHeight:new_height];
                [self reloadInputViews];
            }];
        }
        

        就是这样。

        【讨论】:

        • 您能确认- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height 在您的代码中的位置吗?你打电话给[self constraints][self reloadInputViews] 这意味着self 应该是第一响应者,inputAccessoryView?但是,如果正在编辑,则 textview 必须是第一响应者?
        • @Magoo 是的,self 是第一响应者,inputAccessoryView。我使用了一个可以自动增加高度的 textView,称为 HPGrowingTextView。 - (void)growthTextView:(HPGrowingTextView *)growthTextView willChangeHeight:(float)height 方法会在 textView 改变它的高度时被调用。
        • 我知道这有点牵强,但你能把类/源放在某个地方吗?我不确定当你有一个当前正在编辑的 textView 时 inputAccessoryView 如何成为第一响应者,因此[self reloadInputViews]; 如何不被忽略?
        • 什么是 CommentInputViewBeginHeight?
        【解决方案5】:

        第一个答案并没有完全解决我的问题,但给了我很大的提示。 Apple 确实为附件视图添加了一个私有约束,但您在附件视图的约束列表中找不到它。您必须从其超级视图中搜索它。它杀死了我几个小时。

        【讨论】:

        • @Bannings 通过覆盖 addConstraint() 对我不起作用,因为约束已添加到超级视图中。
        • 相同。这是我解决此问题的实现:- (NSLayoutConstraint *)hiddenInputAccessoryViewHeightConstraint { for (NSLayoutConstraint *constraint in self.myInputAccessoryView.superview.constraints) if (constraint.constant == self.myInputAccessoryView.frame.size.height &amp;&amp; constraint.firstAttribute == NSLayoutAttributeHeight &amp;&amp; constraint != self.myInputAccessoryViewHeightConstraint) return constraint; return nil; }
        【解决方案6】:

        在阅读了上面的答案(这是一个很好的发现)后,我担心依赖您需要更改为 [0]firstObject 的约束是一个实现细节,未来可能会在我们的领导下发生变化。

        在做了一些调试之后,我发现 Apple 在附件输入视图上添加的约束似乎具有 76 的优先级。这是一个疯狂的低值,而不是 @987654323 文档中列出的枚举之一@。

        鉴于priority 这个低值,它似乎是一个更简洁的解决方案,只需有条件地添加/删除另一个具有高优先级的约束,例如UILayoutPriorityDefaultHigh,当您想要调整视图大小时?

        【讨论】:

          猜你喜欢
          • 2015-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-10
          • 1970-01-01
          • 2015-03-13
          • 2015-03-05
          • 2010-12-21
          相关资源
          最近更新 更多