IOS iPhone X 上的标签栏高度问题

IOS Tab Bar height issue on iPhone X

我尝试了很多解决方案,包括 。因此,请在将其标记为重复之前阅读完整问题。对于 iPhone X,我使用安全区域指南来支持 iPhone X 设计。一切都很好,除了 Tab Bar 我仔细检查了它的安全区域限制,但在 运行 之后它的项目仍然散落。

但请注意,标签栏的高度是恒定的,正确的只有其中的项目会受到干扰。

这是我的故事板图片,请不要考虑它的限制和调整。

据我所知,一切都是正确的...但我肯定遗漏了一些东西,请检查,任何建议都会有很大帮助。但请注意,我已经尝试了几乎所有在堆栈或 google.

的前几个链接中提到的方法

这是我用来填充项目图像的代码。

UIColor *color = [UIColor colorWithRed:192.0f/255.0f green:41.0f/255.0f blue:66.0f/255.0f alpha:1.0f];

NSDictionary *textColors = [NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, nil];

for(UITabBarItem *tab in self.tabBar.items)
{
    [tab setTitleTextAttributes:textColors forState:UIControlStateNormal];
    [tab setTitleTextAttributes:textColors forState:UIControlStateSelected];
}
self.tabBar.itemPositioning = UITabBarItemPositioningAutomatic;

[self.tabBar.items[0] setImage:[[UIImage imageNamed:@"search_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[self.tabBar.items[1] setImage:[[UIImage imageNamed:@"user_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[self.tabBar.items[2] setImage:[[UIImage imageNamed:@"call_icon"]
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[self.tabBar.items[3] setImage:[[UIImage imageNamed:@"services_icon"]
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

尝试使用文档中推荐的方法设置项目。

To configure tab bar items directly, use the setItems:animated: method of the tab bar itself.

- (void)setItems:(NSArray<UITabBarItem *> *)items animated:(BOOL)animated;

做这样的事情。

UIIImage *image0 = [UIImage imageNamed:@"search_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem *item0 = [[UITabBarItem alloc] initWithTitle:self.tabBar.items[0].title image:image0 tag:0];
// And so on or in a loop
NSArray<UITabBarItem *> *items = @[ item0, ]; // Add all items here
[self.tabBar setItems:items animated:NO];

在此处阅读更多内容

https://developer.apple.com/documentation/uikit/uitabbar?language=objc

起初我尝试了几乎所有可能的解决方案,但根本不起作用,但最后我以某种方式在底部添加了一些负边距。让我们说“-1”然后一切都开始像魅力一样工作。我还尝试为实验设置正边距。但这没有用,我得出的结论是只有负数有效。这一定是 UIKit 中的一个问题。 设置带安全区域的底部约束,因为任何负数都可以解决此问题。

检查其底部约束。

只需将您的 UITabBar 放入 UIView 中即可!

这对我有用。

这绝对有效。

覆盖 func viewDidLoad() { super.viewDidLoad()

    let numberOfItems = CGFloat(tabBArView.items!.count)
    let tabBarItemSize = CGSize(width: tabBArView.frame.width / numberOfItems, height: 48)

     tabBArView.selectionIndicatorImage =  UIImage.imageWithColor(color: UIColor.orange, size: tabBarItemSize).resizableImage(withCapInsets: UIEdgeInsets.zero)
    tabBArView.frame.size.width = self.view.frame.width + 4
    tabBArView.frame.origin.x = -2

扩展 UIImage {

class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
    let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    color.setFill()
    UIRectFill(rect)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return image
}