JsqMessagesViewController 输入工具栏 IOS 11 iPhone X Swift4

4

我知道这个库已经过时了,但是有没有人解决了inputToolBar和iPhone X的问题?目前inputToolBar部分被覆盖。请查看附图。

enter image description here

提前感谢


{btsdaf} - Tore Olsen
{btsdaf} - Sami Ali
{btsdaf} - Tore Olsen
FYI:我现在已经更新了答案。 - Tore Olsen
6个回答

19

如果您不想更改 Pod 中的代码并使用更符合 Swift 风格的扩展,您可以使用以下代码:

extension JSQMessagesInputToolbar {
    override open func didMoveToWindow() {
        super.didMoveToWindow()
        guard let window = window else { return }
        if #available(iOS 11.0, *) {
            let anchor = window.safeAreaLayoutGuide.bottomAnchor
            bottomAnchor.constraintLessThanOrEqualToSystemSpacingBelow(anchor, multiplier: 1.0).isActive = true
        }
    }
}

5
出现了 "Static member 'activate' cannot be used on instance of type 'NSLayoutConstraint'" 的错误。请将其替换为 ".isActive = true"。 - Nike Kov
聪明的回答,你救了我。谢谢 :) - steveSarsawa

13

更新了 Samson 的答案,因为当我离开屏幕时会崩溃。在设置约束之前检查它不是 nil。

-(void) didMoveToWindow{
    [super didMoveToWindow];
    if (@available(iOS 11.0, *)) {

        UILayoutGuide *layoutGuide = self.window.safeAreaLayoutGuide;

        if (layoutGuide != nil){
           [[self bottomAnchor] constraintLessThanOrEqualToSystemSpacingBelowAnchor:layoutGuide.bottomAnchor multiplier:1.0].active = YES;
        }

    }
}

8

各位,我是一个提问 jsqmessageviewcontroller ios11 toolbar 的人,我已经解决了!

只需要将以下代码放入JSQMessagesInputToolbar.m文件中。似乎输入工具栏被放置在它自己的窗口中,您需要单独访问它的窗口。

-(void) didMoveToWindow{
[super didMoveToWindow];
 if (@available(iOS 11.0, *)) {
     [[self bottomAnchor] constraintLessThanOrEqualToSystemSpacingBelowAnchor:self.window.safeAreaLayoutGuide.bottomAnchor multiplier:1.0].active = YES;
     }
}

1
{btsdaf} - Sami Ali
{btsdaf} - Samson Wong

5
我认为以下步骤是解决这个问题的更好方法:
1. 打开JSQMessagesViewController.xib文件
2. 将集合视图的底部布局约束拖动到JSQMessagesViewController.m中,命名为collectionViewBottomLayoutGuide。
3. 将以下代码添加到JSQMessagesViewController.m文件的底部。
- (void)viewSafeAreaInsetsDidChange {
    [super viewSafeAreaInsetsDidChange];
    self.toolbarBottomLayoutGuide.active = NO;
    self.toolbarBottomLayoutGuide = [NSLayoutConstraint constraintWithItem:self.view.safeAreaLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.inputToolbar attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
    self.toolbarBottomLayoutGuide.active = YES;
    self.collectionViewBottomLayoutGuide.active = NO;
    self.collectionViewBottomLayoutGuide = [NSLayoutConstraint constraintWithItem:self.view.safeAreaLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.collectionView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
    self.collectionViewBottomLayoutGuide.active = YES;
}

在搜索了两个小时后,我找到了最好的解决方案!谢谢! - SDW
这是完美的。通过扩展解决方案计算collectionView高度是不正确的。 - Leonardo

3
增加新的约束并保留旧的约束,这将导致在xcode调试屏幕上看到布局错误,xcode将破坏其中一个约束以满足布局。即使您在屏幕上看到正确的布局,您可能会遇到不同的问题,例如不响应键盘更改。
通过以下步骤更改旧约束可以更好地解决这个问题:
  1. 打开JSQMessagesViewController.xib
  2. 点击文件检查器并选择“使用安全区域布局指南”
  3. 将JSQMessagesViewController的部署目标更改为9.0
  4. 将输入工具栏底部约束的第一个项目从Superview更改为Safe Area,并将常数更改为0
  5. 运行您的项目。

当然,那是更好的方法,但我尝试过后发现工具栏无法随键盘一起上移。你有成功过吗? - Sami Ali
非常好的解决方案!对我有用。谢谢伙计。@SamiAli,你应该先将工具栏向上拖动一点,然后将其底部约束设置为安全区域。 - Suhail

2
稍微改进了@J-Bossi的答案,对于我们这些无法处理自动布局技术破坏的人来说...
只需先删除现有的约束条件,然后再添加新的约束条件。
extension JSQMessagesInputToolbar {
    override open func didMoveToWindow() {
        super.didMoveToWindow()
        guard let window = window else { return }
        if #available(iOS 11.0, *) {
            guard let constraint = (superview?.constraints.first { $0.secondAnchor == bottomAnchor }) else { return }
            let anchor = window.safeAreaLayoutGuide.bottomAnchor
            NSLayoutConstraint.deactivate([constraint])
            bottomAnchor.constraintLessThanOrEqualToSystemSpacingBelow(anchor, multiplier: 1.0).isActive = true
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接