ios - 为什么页面推送动画 Tabbar 在 iPhone X 上移

标签 ios ios11 iphone-x

我构建了一个应用程序 Demo,使用 hidesBottomBarWhenPushed 在推送动画中隐藏 Tabbar。

Page Relationship

但是,当我点击跳转按钮时,Tabbar 会向上移动!?像这样: Tabbar Move up

最佳答案

VoidLess 提供的答案仅部分修复了 TabBar 问题。它修复了标签栏内的布局问题,但是如果您使用隐藏标签栏的 View Controller ,则标签栏在动画期间呈现不正确(要重现它最好有 2 个 segues - 一个模态和一个推送。如果你交替 segues,你可以看到 tabbar被渲染错位)。请参阅下面解决这两个问题的代码。干得好,苹果。

class SafeAreaFixTabBar: UITabBar {

var oldSafeAreaInsets = UIEdgeInsets.zero

@available(iOS 11.0, *)
override func safeAreaInsetsDidChange() {
    super.safeAreaInsetsDidChange()

    if oldSafeAreaInsets != safeAreaInsets {
        oldSafeAreaInsets = safeAreaInsets

        invalidateIntrinsicContentSize()
        superview?.setNeedsLayout()
        superview?.layoutSubviews()
    }
}

override func sizeThatFits(_ size: CGSize) -> CGSize {
    var size = super.sizeThatFits(size)
    if #available(iOS 11.0, *) {
        let bottomInset = safeAreaInsets.bottom
        if bottomInset > 0 && size.height < 50 && (size.height + bottomInset < 90) {
            size.height += bottomInset
        }
    }
    return size
}

override var frame: CGRect {
    get {
        return super.frame
    }
    set {
        var tmp = newValue
        if let superview = superview, tmp.maxY != 
        superview.frame.height {
            tmp.origin.y = superview.frame.height - tmp.height
        }

        super.frame = tmp
        }
    }
}
}

objective-c 代码:

@implementation VSTabBarFix {
    UIEdgeInsets oldSafeAreaInsets;
}


- (void)awakeFromNib {
    [super awakeFromNib];

    oldSafeAreaInsets = UIEdgeInsetsZero;
}


- (void)safeAreaInsetsDidChange {
    [super safeAreaInsetsDidChange];

    if (!UIEdgeInsetsEqualToEdgeInsets(oldSafeAreaInsets, self.safeAreaInsets)) {
        [self invalidateIntrinsicContentSize];

        if (self.superview) {
            [self.superview setNeedsLayout];
            [self.superview layoutSubviews];
        }
    }
}

- (CGSize)sizeThatFits:(CGSize)size {
    size = [super sizeThatFits:size];

    if (@available(iOS 11.0, *)) {
        float bottomInset = self.safeAreaInsets.bottom;
        if (bottomInset > 0 && size.height < 50 && (size.height + bottomInset < 90)) {
            size.height += bottomInset;
        }
    }

    return size;
}


- (void)setFrame:(CGRect)frame {
    if (self.superview) {
        if (frame.origin.y + frame.size.height != self.superview.frame.size.height) {
            frame.origin.y = self.superview.frame.size.height - frame.size.height;
        }
    }
    [super setFrame:frame];
}


@end

关于ios - 为什么页面推送动画 Tabbar 在 iPhone X 上移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232929/

相关文章:

ios - 在 Firemonkey 的 iOS 应用程序中使用 SOAP Web 服务

python - 使用 Keras 2 保存的 CoreML 加载模型

ios - 构建应用程序时 Xcode 9 Main.storyboard 错误 "Internal error. Please file a bug at bugreport.apple.com and attach "

ios - iPhone X 上的 UITabBar selectionIndicatorImage 高度

ios - 苹果手机 x : keep search bar within safe zone

ios - 安全区域布局指南不适用于 Storyboard中的 UITableViewController

ios - 当亚马逊的 iOS SDK v2 仍处于开发者预览版时,我们可以在实时应用程序中使用它吗?

ios - swift 核心数据 : Best approach for showing specific data?

ios - 如何将选项卡栏 Controller 添加到 iOS 中的 subview ?

ios - Xcode9无线调试上不了大学网