LoginSignup
0
1

More than 3 years have passed since last update.

UITabBarItemで改行が効かなかった

Posted at

概要

UITabBarのUITabItemで改行が効きませんでした。
久しぶりにswift触ったので改行コード自体を¥nとか書いちゃったけどw
.titleに\nで入れても効きませんでした。

ggr

ググると英語圏で下記の記事
https://stackoverflow.com/questions/46219860/multiple-lines-in-uitabbaritem-label
いちいちUITabBarのsubviewからUILabelを取り出して、設定してあげる必要があるらしい:dizzy_face:

結果

下記のようなメソッドを作って設定しました:expressionless:
とりあえずちゃんと設定されたけど、なんか違うような気もするなぁー・・・

    /**
     * タブアイテムの文字列改行
     */
    func tabItemNewLineText(menu_title: String, tab_number :Int) {
        let viewTabBar = self.tabbar.items![tab_number].value(forKey:"view") as? UIView
        let label = viewTabBar?.subviews[1] as? UILabel
        label?.numberOfLines = 0
        label?.textAlignment = NSTextAlignment.center
        // sizeToFitで上手く横幅が取れなかったため固定値で指定
        label?.frame = CGRect(x: (label?.frame.origin.x)!, y: (label?.frame.origin.y)!, width: 100, height: 30)
//        label?.sizeToFit()
        label?.text = menu_title
    }
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1