Multiple lines in UITabBarItem Label

无人久伴 提交于 2019-12-02 18:46:19

问题


I've tried many things but impossible to find a way to put the label of a UITabBarItem with a lineNumber customised.0 (i would like to get the title on 2 lines).

Is there a way to do it?


回答1:


Now it contains two subviews. At 0 it is imageView and at 1 it is label. Now make the height of imageview a bit smaller so that you can give the height of label a bit larger to have multiple lines. Set the property ofnumberoflines of the label to 0 via code.

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

and now play with this label.




回答2:


More stable solution:

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:


you can not do this with UIBarButtonItem because it doesn't have property titleView like UINavigationItem!

You can set only string as title and tab image! that's it!

If you have option to set label as titleview of tabbaritem then you can take label with numberofline 0 but here you can set string only!



来源:https://stackoverflow.com/questions/46219860/multiple-lines-in-uitabbaritem-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!