如果您不想更改 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
}
}
}
更新了 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;
}
}
}
各位,我是一个提问 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;
}
}
- (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;
}
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
}
}
}