为什么单击它时UIBarButtonItem会调整大小?

时间:2018-05-04 13:43:41

标签: ios swift

每当我点击这个具有unicode标题的UIBarButtonItem来显示一个cog符号时,它会调整大小并变小。这是代码:

let settings = UIBarButtonItem(title: NSString(string: "\u{2699}\u{0000FE0E}") as String, style: .plain, target: self, action: #selector(show_settings))
let font = UIFont.systemFont(ofSize: 28)
let attributes = [NSAttributedStringKey.font : font]
settings.setTitleTextAttributes(attributes, for: .normal)

当我点击它时它看起来如何: UIBarButtonItem resize bug

4 个答案:

答案 0 :(得分:1)

为突出显示的状态以及以下设置TitleAttribute:

 let settings = UIBarButtonItem(title: NSString(string: "\u{2699}\u{0000FE0E}") as String, style: .plain, target: self, action: #selector(show_settings))
        let font = UIFont.systemFont(ofSize: 28)
        let attributes = [NSAttributedStringKey.font : font]
        settings.setTitleTextAttributes(attributes, for: .normal)
        settings.setTitleTextAttributes(attributes, for: .highlighted)

enter image description here

希望这会对你有所帮助:)。

答案 1 :(得分:0)

您可以在此处为正常状态

设置属性
settings.setTitleTextAttributes(attributes, for: .normal)

答案 2 :(得分:0)

试试这个

     settings.titleLabel.font = [UIFont systemFontOfSize: 28];

或Swift

    settings.titleLabel?.font =  UIFont.systemFont(ofSize: 28)

我认为问题在于你正常使用这个属性,而当你点击它时,max是默认大小会覆盖它。我不确定,但上面的一行应该有用

答案 3 :(得分:0)

试试这个,

settings.setTitleTextAttributes(
            [
                NSAttributedStringKey.font :UIFont.systemFont(ofSize: 28) ,
                NSAttributedStringKey.foregroundColor : UIColor.blue
            ],
            for: .normal)