ios 如何纠正 iPhone X 上的 Tab Bar 高度问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47054707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:37:06  来源:igfitidea点击:

How to correct Tab Bar height issue on iPhone X

iosswiftxcodeuistoryboardiphone-x

提问by icekomo

I'm having an issue with my app when testing for iPhone X. I'm not sure how to adjust this issue, as well as not make it an issue for non iPhone X sizes. This only seems to be an issue on the iPhone X simulator.

我在测试 iPhone X 时遇到了我的应用程序问题。我不知道如何调整这个问题,也不知道如何让它成为非 iPhone X 尺寸的问题。这似乎只是 iPhone X 模拟器上的问题。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

采纳答案by Dipak Kacha

"File inspector"

“档案检查员”

"File inspector" from right of Xcode storyboard, enable Safe Area guide layout to support your app in iPhone

Xcode 故事板右侧的“文件检查器”,启用安全区域指南布局以支持您在 iPhone 中的应用

This postdescribes it really well.

这篇文章描述得非常好。

回答by Andrey

On iOS 12.1I've solved this issue by overriding safeAreaInsets in the UITabBar subclass:

iOS 12.1 上,我通过覆盖 UITabBar 子类中的 safeAreaInsets 解决了这个问题:

class TabBar: UITabBar {
    private var cachedSafeAreaInsets = UIEdgeInsets.zero

    override var safeAreaInsets: UIEdgeInsets {
        let insets = super.safeAreaInsets

        if insets.bottom < bounds.height {
            cachedSafeAreaInsets = insets
        }

        return cachedSafeAreaInsets
    }
}

回答by Mohamed Ali

Create a separate file with the following code:

使用以下代码创建一个单独的文件:

extension UITabBar {
    override open func sizeThatFits(_ size: CGSize) -> CGSize {
        super.sizeThatFits(size)
        guard let window = UIApplication.shared.keyWindow else {
            return super.sizeThatFits(size)
        }
        var sizeThatFits = super.sizeThatFits(size)
        sizeThatFits.height = window.safeAreaInsets.bottom + 40
        return sizeThatFits
    }
}

回答by clopex

For iOS 11.3 this worked for me:

对于 iOS 11.3,这对我有用:

func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    tabBar.invalidateIntrinsicContentSize()
}

回答by Randeep

I had a a similar issue. I was setting the selectionIndicatorImage in viewDidLoad(). Moving the code to viewDidLayoutSubviews() fixed my issue.

我有一个类似的问题。我在 viewDidLoad() 中设置了 selectionIndicatorImage。将代码移动到 viewDidLayoutSubviews() 解决了我的问题。

回答by Mehul Thakkar

In Constraints -

在约束条件下 -

If you are giving bottom space with "Bottom Layout Guide", then this issue will occur.

如果您使用“底部布局指南”提供底部空间,则会出现此问题。

Solution:

解决方案:

Give bottom space with respect to superview. This will work 100% perfect.

为超级视图留出底部空间。这将 100% 完美运行。

回答by Jonah

This worked for me.

这对我有用。

[self.tabBar.bottomAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.bottomAnchor].active = YES;

回答by Jonah

Follow below guidelines for setting the UITabbar selectionIndicatorImage.

按照以下指南设置 UITabbar selectionIndicatorImage。

  1. UITabBar.appearance().selectionIndicatorImage = #YOUR_IMAGE
  2. Make sure your image height is 48.
  1. UITabBar.appearance().selectionIndicatorImage = #YOUR_IMAGE
  2. 确保您的图像高度为 48。

The default height of tabbar selectionIndicatorImage is 49, But in iPhone X set image height equals to 48.

tabbar selectionIndicatorImage 的默认高度为 49,但在 iPhone X 中设置图像高度等于 48。

回答by Gytis

The solution for me was that I had a custom UITabBar height set, something like this:

我的解决方案是我有一个自定义的 UITabBar 高度集,如下所示:

  override func viewWillLayoutSubviews() {            
        var tabFrame = tabBar.frame
        tabFrame.size.height = 60
        tabFrame.origin.y = self.view.frame.size.height - 60
        tabBar.frame = tabFrame
    }

Remove it and the tab bar will display correctly on iPhone X.

删除它,标签栏将在 iPhone X 上正确显示。

回答by ben87nz

Just align the bottom of the UITabBar to the superview, not to the safe area. If you align it to safe area it will be like this:

只需将 UITabBar 的底部与超级视图对齐,而不是与安全区域对齐。如果将其与安全区域对齐,它将是这样的:

Align to safe area

对齐到安全区域

And when aligned to the superview, it will show correctly:

当与超级视图对齐时,它将正确显示:

Align to Superview

对齐超级视图

I think this is because Apple gave the tab bar items a default margin to the bottom if it is on iPhone X as they want the tab bar to be extended to the bottom of the screen to avoid a floating bar.

我认为这是因为如果在 iPhone X 上,Apple 为标签栏项目提供了底部的默认边距,因为他们希望标签栏扩展到屏幕底部以避免浮动栏。