繁体   English   中英

UITabBarItem 标签中的多行

[英]Multiple lines in UITabBarItem Label

我已经尝试了很多东西,但无法找到一种方法来放置带有 lineNumber customised.0 的 UITabBarItem 标签(我想在 2 行上获得标题)。

有没有办法做到这一点?

现在它包含两个子视图。 0 是 imageView,1 是标签。 现在使imageview的高度变小一点,以便您可以将标签的高度设置得更大一些,以便有多行。 通过代码将label的ofnumberoflines属性设置为0。

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

现在玩这个标签。

更稳定的解决方案:

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 }

这是我的解决方案

我们需要在 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)
        }
    }
}

这是结果在此处输入图片说明

你不能用UIBarButtonItem这样做,因为它没有像UINavigationItem这样的属性titleView

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM