【问题标题】:Multiple lines in UITabBarItem LabelUITabBarItem 标签中的多行
【发布时间】:2017-09-14 12:58:33
【问题描述】:

我尝试了很多方法,但无法找到一种方法将 UITabBarItem 的标签与 lineNumber customised.0(我想将标题放在 2 行)。

有办法吗?

【问题讨论】:

  • @chirag90 这是关于导航栏的标题,而不是标签栏项目的标题。
  • 你想要 UITabBarItem 中的标签意味着你也想要它的触摸事件(我猜)?那么你就可以直接使用多行的 UIButton 了。
  • “标题标签”未暴露...您几乎肯定需要使用自定义标签栏。

标签: ios swift uitabbaritem


【解决方案1】:

现在它包含两个子视图。在 0 处是 imageView,在 1 处是标签。 现在将imageview 的高度调小一点,这样您就可以将标签的高度调大一点,以便拥有多行。通过代码设置标签的属性ofnumberoflines为0。

let viewTabBar = tabBarItem.value(forKey: "view") as? UIView
let imageView = viewTabBar?.subviews[0] as? UIImageView
let label = viewTabBar?.subviews[1] as? UILabel

现在玩这个标签。

【讨论】:

  • 你应该为此获得一枚奖章;)我很确定你的回答会帮助很多人!
  • 注意,如果你没有图片,UILabel的索引将为0,我在下面发布了更通用的解决方案
  • 你能解释一下吗,键'view'的值是什么?当我运行此代码时,我将 viewTabBar 设为 nil。
  • 这是一个不完整的答案,没有证据证明它有效。你如何向上移动图像和标签,使标签栏更高,然后占两行?所有这些都显示了 TabBarItems 如何具有子视图。这不是答案。
  • @Arnav 参考您的答案,我可以问问题tabbar item title 相关吗?
【解决方案2】:

更稳定的解决方案:

guard let view = tabBarItem?.value(forKey: "view") as? UIView,
        let label = (view.subviews.flatMap{ $0 as? UILabel }).first,
        let imageView = (view.subviews.flatMap{ $0 as? UIImageView }).first else { return }

【讨论】:

    【解决方案3】:

    这是我的解决方案

    我们需要在 viewwillLayoutSubviews 中实现这个。更新用户界面并使其正常工作

    在此示例中,仅自定义我的第三个标签栏项目

    override func viewWillLayoutSubviews() {
        // acess to list of tab bar items
        if let items = self.tabBar.items {
            // in each item we have a view where we find 2 subviews imageview and label
            // in this example i would like to change
            // access to item view
            let viewTabBar = items[2].value(forKey: "view") as? UIView
            // access to item subviews : imageview and label
            if viewTabBar.subviews.count == 2 {
                let label = viewTabBar?.subviews[1]as? UILabel
                // here is the customization for my label 2 lines
                label?.numberOfLines = 2
                label?.textAlignment = .center
                label!.text = "tab_point".localized
                // here customisation for image insets top and bottom
                items[2].imageInsets = UIEdgeInsets(top: 8, left: 0, bottom: -5, right: 0)
            }
        }
    }
    

    这是结果

    【讨论】:

      【解决方案4】:

      UIBarButtonItem 不能这样做,因为它没有 titleViewUINavigationItem 这样的属性!

      您只能将字符串设置为标题和标签图像!就是这样!

      如果您可以选择将标签设置为 tabbaritem 的标题视图,那么您可以使用 numberofline 0 的标签,但在这里您只能设置字符串!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-20
        • 2016-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多