【问题标题】:IOS Tab Bar height issue on iPhone XiPhone X上的IOS标签栏高度问题
【发布时间】:2018-02-01 11:31:07
【问题描述】:

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

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

这是我的故事板的图像,请不要限制和调整。

据我所知,一切都是正确的......但我肯定错过了一些东西,请检查,任何建议都会有很大帮助。但请不要我尝试了堆栈或谷歌前几个链接中提到的几乎所有方法。

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

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]];

【问题讨论】:

    标签: objective-c height uitabbar iphone-x


    【解决方案1】:

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

    检查它的底部约束。

    【讨论】:

      【解决方案2】:

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

      要直接配置标签栏项目,请使用 setItems:animated: 方法 标签栏本身。

      - (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

      【讨论】:

      • 奇怪。如果仅通过 Storyboard 进行配置,它是否有效?
      • 您可以尝试取消选中标签栏的Safe area relative margins 吗?如果这不起作用,请尝试取消选中Preserve superviews margins
      • 当然..让我这样做
      • 老兄,你不会相信什么对我有用.....我将底部约束设置为安全区域的 -1 并且它有效.....我也尝试过 +1,0 但它不仅 -1 及以后的任何负数都可以正常工作
      【解决方案3】:

      只需将您的 UITabBar 放在 UIView 中!

      这对我有用。

      【讨论】:

        【解决方案4】:

        这肯定会奏效。

        覆盖 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
        }
        

        【讨论】:

          猜你喜欢
          • 2018-04-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-12
          • 2010-12-03
          相关资源
          最近更新 更多