【问题标题】:Moving UITabBarItem Image down?向下移动 UITabBarItem 图像?
【发布时间】:2013-04-23 12:09:13
【问题描述】:

通常在UITabBar 的每个选项卡上都有一个小图像和一个命名选项卡的标题。图像位于标签顶部/居中以容纳下方的标题。我的问题是:如果你想要一个只有图片而没有标题的 tabBar,有没有办法将图片向下移动,以便在标签内更好地居中?

我目前正在使用(见下文):

[tabBarItem setFinishedSelectedImage:tabSelected withFinishedUnselectedImage:tabUnselected];

但更喜欢使用没有标题的较大图像,目前如果我使图像大于大约 70pixels@2x,它会开始从 UITabBar 的顶部边缘移开,同时在底部留下大量未使用的空间.

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitabbar uitabbaritem


    【解决方案1】:

    尝试调整tabBarItemimageInsets(用于移动图标图像)并将控制器标题设置为零(因此不显示标题)。把这样的东西放到视图控制器中的-init-viewDidLoad 方法中:

    Objective-C

    self.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    self.title = nil;
    

    斯威夫特

    self.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
    self.title = nil
    

    UITabBarItemUIBarItem 的子类,具有UIEdgeInsets imageInsets 属性。玩一点插图,直到它看起来不错(取决于您的标签栏图标图像)

    【讨论】:

    • @Lukas:但这会缩小图像。
    • @Sourabh 不会为我缩小它们
    • 如果需要像素完美,实际插入值为 5.5f。
    • @rr1g0 无法将徽章移动到自定义位置。如果您需要自定义徽章位置或自定义徽章外观,则必须自己实现
    • @Sourabh 你必须用负值偏移底部才能不缩小图像。
    【解决方案2】:

    创建UITabBarController 的子类,并在其viewDidLoad 中:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.viewControllers enumerateObjectsUsingBlock:^(UIViewController *vc, NSUInteger idx, BOOL *stop) {
            vc.tabBarItem.title = nil;
            vc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
        }];
    }
    

    斯威夫特 3:

    for vc in self.viewControllers! {
        vc.tabBarItem.title = nil
        vc.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
    }
    

    【讨论】:

    • 这对我有用。只是为了快速,方法 enumerateObjectsUsingBlock 不再可用
    • 不要子类化,而是把它放到你的 AppDelegate 中
    • 将这个 sn-p 放入 AppDelegate 也可以,但它显然并不比继承它好。不过你可以有自己的喜好:)
    【解决方案3】:

    如果您使用的是 Xamarin,则可以:

    screen.TabBarItem.ImageInsets = new UIEdgeInsets(5, 0, -5, 0);
    screen.TabBarItem.Title = "";
    

    【讨论】:

      【解决方案4】:

      您也可以通过情节提要来完成。选择您的 tabbaritem,转到尺寸检查器并分配适当的插图。

      *在 Xcode 版本 7.3.1 (7D1014) 上演示

      【讨论】:

      • 又好又干净!
      • 最好的答案...非常感谢@Neil Galiaskarov
      【解决方案5】:

      SWIFT 3.0

      您可以设置图像的lnsets,根据设计设置顶部、左侧、底部和右侧。

      self.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: 0, right: 0)
      

      【讨论】:

        【解决方案6】:

        对于 iOS 11,除了设置 ImageInsets 之外,您还需要重写 TraitCollection 方法。 请在您的子类 UITabBarController 类中添加该方法

        public override UITraitCollection TraitCollection {
         get {
          return UITraitCollection.FromHorizontalSizeClass(horizontalSizeClass: UIUserInterfaceSizeClass.Compact);
         }
        }
        

        【讨论】:

        • 很好的提示,帮助我解决了与 iPad 和 iOS 11 上标签栏项目的图像位置相关的问题。但是在视图控制器级别覆盖特征集合太苛刻了,最好是子类化UITabBar 类并覆盖那里的 traitCollection 属性。同样返回只有水平类的特征集合,将删除其他特征集合属性。 return UITraitCollection(traitsFrom: [ super.traitCollection, UITraitCollection(horizontalSizeClass: .compact)]) 帮了我大忙。
        【解决方案7】:

        这对我有用

        斯威夫特 4

        let array = tabBarController?.customizableViewControllers
        for controller in array! {
            controller.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0)
        }
        

        【讨论】:

          【解决方案8】:

          Swift 4.2 中,UIEdgeInsetsMake 已被弃用,我们应该使用 UIEdgeInsets

          let array = tabBarController?.customizableViewControllers
              for controller in array! {
                  controller.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
              }
          

          【讨论】:

            【解决方案9】:

            这些对我不起作用,唯一有效的解决方案是在添加图像时设置 alignmentRectInsets。这是我的样子:

            let newsView = NewsViewController()
            let newsIcon = UITabBarItem(title: nil, image: UIImage(systemName: "newspaper")?.withAlignmentRectInsets(UIEdgeInsets(top: 8.5, left: 0, bottom: -8.5, right: 0)), tag: 0)
            newsView.tabBarItem = newsIcon
            let viewControllers = [newsNavController]
            self.viewControllers = viewControllers
            

            【讨论】:

              【解决方案10】:

              2021 年,Objective-C

              我尝试了所有方法,没有一种方法有效。最终我发现这是因为使用 SF 符号作为 tabbaritem 图像。如果我用自定义图像替换它,一切正常。

                  NSArray *tabItems = [self.tabBar items];
                  // set tabItem icon, remove blue image with render mode set.
                  UIImage *image1 = [[UIImage imageNamed:@"myIcon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
                  
                  // set title offset
                  [tabItems[0] setTitlePositionAdjustment:UIOffsetMake(0, 20)];
                  [tabItems[1] setTitlePositionAdjustment:UIOffsetMake(-20, 20)];
                  [tabItems[2] setTitlePositionAdjustment:UIOffsetMake(20, 20)];
                  [tabItems[3] setTitlePositionAdjustment:UIOffsetMake(0, 20)];
              
                  // set image offset
                  [tabItems[0] setImageInsets:UIEdgeInsetsMake(0, 0, -30, 0)];
                  ...
              

              对于 uitabbaritem 图像分辨率,@2x 为 50x50px,@3x 为 75x75px。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-09-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多