【问题标题】:How to move title of UITabBarItem?如何移动 UITabBarItem 的标题?
【发布时间】:2012-05-13 22:57:51
【问题描述】:

谁能告诉我如何将 UITabBarItem 的标题(例如 2px)移到顶部?

【问题讨论】:

    标签: iphone objective-c xcode uitabbar uitabbaritem


    【解决方案1】:

    解决方案:

    UITabBarItem *item = [tabBar.items objectAtIndex:0]; 
    item.titlePositionAdjustment = UIOffsetMake(0, -5.0);
    

    【讨论】:

    • 在 swift 中令人惊讶:item.setTitlePositionAdjustment(UIOffsetMake(0, -5.0))(XCode 6.1 中只有 setter 函数)
    • 在 Swift2/Xcode7 中,setter 函数消失了,您可以使用 normal 属性来设置值。 item.titlePositionAdjustment = UIOffset(horizontal:0,vertical:1000)
    【解决方案2】:

    Swift 3 更新

    将此代码放入UITabBarController:

    UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -5)
    

    归功于Tim Brown

    【讨论】:

      【解决方案3】:

      斯威夫特 4 像我一样,如果您在添加所有控制器后在其他控制器中创建标签栏控制器,您可以循环浏览标签栏项目并更改标题、定位和字体

      for (index,tabBarItem) in tabBarController.tabBar.items!.enumerated() {
              tabBarItem.image = nil //if you only want title and no Image
              tabBarItem.title = titleArray[index] //setting your title based on some array
              let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20)] // changeing font and height of the text
              tabBarItem.setTitleTextAttributes(attributes, for: .normal)
              tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10.0) // updating the title positon 
          } 
      

      【讨论】:

        【解决方案4】:

        您可以使用tabbarItem.titlePositionAdjustment进行调整。

        如果您想用它做更多事情,请访问 tabbaritem 子视图并在 tabbar.subviews 上找到标签。例如

         int i = 0;
        for (NSObject *view in self.tabBar.subviews)
        {
            MTLog(@"%@", view);
            if ([view respondsToSelector:@selector(subviews)])
            {
                for (NSObject *childView in ((UIView *)view).subviews)
                {
                    if ([childView isKindOfClass:[UILabel class]])
                    {
                        if (i > (titlesArray.count - 1))
                            break;
                        UILabel *label = (UILabel *)childView;
                        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titlesArray[i] attributes:@{
                                                                                                                                                    NSFontAttributeName:[UIFont systemFontOfSize:9]
                                                                                                                                                    }];
        
                        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
                        [style setMinimumLineHeight:26];
                        [style setAlignment:NSTextAlignmentRight];
        
                        [attributedString addAttribute:NSParagraphStyleAttributeName
                                                 value:style
                                                 range:NSMakeRange(0, attributedString.length)];
                        label.attributedText = attributedString;
                        i++;
                    }
        
                }
            }
        
        
        }
        

        【讨论】:

          【解决方案5】:

          你不能...你可以破解它,但这并不容易,并且可能会在未来的 iOS 更新中破坏你的应用程序。

          最好的办法是创建自己的 UITabBar 系统...这是一个很好的指南,您可以看看..

          http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/

          【讨论】:

          • 我找到了这个解决方案: UITabBarItem *item = [tabBar.items objectAtIndex:0]; item.titlePositionAdjustment = UIOffsetMake(0, -5.0);
          • 啊.. 抱歉,上次我寻找此功能时它不存在...阅读它的类参考现在它仅适用于 iOS5 +,因此您可能需要检查该功能如果您的应用程序设计用于 iOS 5 之前的版本,则在调用它之前可用...您可以这样做...if ([item respondsToSelector:@selector(setTitlePositionAdjustment:)]) { item.titlePositionAdjustment = UIOffsetMake(0, -5.0); }
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-04-28
          • 1970-01-01
          • 2011-04-01
          • 2011-07-31
          • 1970-01-01
          • 2013-04-23
          • 1970-01-01
          相关资源
          最近更新 更多