【问题标题】:How to fix the size of UIBarButtonItem when its font weight changes如何在 UIBarButtonItem 的字体粗细发生变化时修复其大小
【发布时间】:2021-09-25 08:16:41
【问题描述】:

以下是从我的项目中提炼出来的一小段演示代码。它由一个 UINavigationBar、UINavigationItem 和几个 UIBarButtonItems 组成。点击 barButtonItem 会突出显示它并使其他变暗。这样做,字体大小发生变化是可以理解的,但我希望 barButtonItems 本身的大小是固定的。这样,当我随机点击 barButtonItems 时,它们似乎不会随着标题文本的收缩和扩展而抖动。如何让 barButtonItems 在标题文本更改时抵抗调整大小?

import UIKit

class ViewController: UIViewController {
    
    var barButtonItems: [UIBarButtonItem]!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .darkGray
        
        let navBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 100, width: view.frame.width, height: 40))
        navBar.barTintColor = #colorLiteral(red: 0.1019607857, green: 0.2784313858, blue: 0.400000006, alpha: 1)
        view.addSubview(navBar)
        
        let navItem = UINavigationItem()
        let appleBarBtnItem  = UIBarButtonItem(title: "Apple", style: .plain, target: self, action: #selector(barItemTapped))
        let bananaBarBtnItem = UIBarButtonItem(title: "Banana", style: .plain, target: self, action: #selector(barItemTapped))
        let kiwiBarBtnItem = UIBarButtonItem(title: "Kiwi", style: .plain, target: self, action: #selector(barItemTapped))
        let pearBarBtnItem = UIBarButtonItem(title: "Pear", style: .plain, target: self, action: #selector(barItemTapped))
        let orangeBarBtnItem = UIBarButtonItem(title: "Orange", style: .plain, target: self, action: #selector(barItemTapped))
        barButtonItems = [appleBarBtnItem, bananaBarBtnItem, kiwiBarBtnItem, pearBarBtnItem, orangeBarBtnItem]

        navItem.leftBarButtonItems = barButtonItems
        navBar.setItems([navItem], animated: false)
        
        setCurrentBarItem(appleBarBtnItem)
    }
    
    func setCurrentBarItem(_ barBtnItem: UIBarButtonItem) {
        for btn in barButtonItems {
            highlight(btn, turnedOn: (btn == barBtnItem) )
        }
    }
    
    func highlight(_ button: UIBarButtonItem, turnedOn: Bool) {
        button.tintColor = #colorLiteral(red: 0.7230747342, green: 0.9554787278, blue: 0.9893732667, alpha: 1)
        let weight: UIFont.Weight = turnedOn ? .bold : .thin
        let attributes: [NSAttributedString.Key : Any] = [ .font: UIFont.systemFont(ofSize: 20, weight: weight)]
        button.setTitleTextAttributes(attributes, for: .normal)
    }
    
    @objc func barItemTapped(_ sender: UIBarButtonItem) {
        setCurrentBarItem(sender)
    }
}

【问题讨论】:

    标签: swift size uibarbuttonitem


    【解决方案1】:

    他们摇摆不定的一个原因是您只为.normal 状态设置setTitleTextAttributes。也为.selected 添加它,这样它的抖动就会减少,但它不会完全解决您的问题。 像这样的东西

    button.setTitleTextAttributes(attributes, for: .selected)
    

    我建议尝试为 UIBarButtonItems 的 ui 文本保持器元素添加一些边距。可以修复它!

    【讨论】:

    • 不,这没有帮助。看起来我必须放弃为选定的 barButtonItem 设置 .bold 字体粗细,而在没有设置时使用 .thin 字体粗细,因为字体粗细的变化会扩大或缩小 barButtonItem 的宽度。我最终决定简单地改变 barButtonItems 的颜色并提供一个简单的动画。
    【解决方案2】:

    看起来我必须放弃为选定的 barButtonItem 设置 .bold 字体粗细,而如果不是,我必须放弃 .thin 字体粗细,因为字体粗细的变化会扩大或缩小 barButtonItem 的宽度,从而导致并列当我点击它们时,barButtonItems 可以移动和晃动。我最终决定只改变 barButtonItems 的颜色并提供一个简单的动画:

        func highlight(_ button: UIBarButtonItem, turnedOn: Bool) {
            let tintColor = turnedOn ? 
                #colorLiteral(red: 0.7230747342, green: 0.9554787278, blue: 0.9893732667, alpha: 1) 
                : 
                #colorLiteral(red: 0.1901655495, green: 0.5488880277, blue: 0.7976593971, alpha: 1)
            
            UIView.animate(withDuration: 0.2, 
                           delay: 0, 
                           options: [.allowUserInteraction, .curveEaseInOut], 
                           animations: { button.tintColor = tintColor }, 
                           completion: nil)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 2014-03-02
      • 2012-07-12
      • 2021-05-14
      相关资源
      最近更新 更多