UIBarButton位置更改图标更改

时间:2017-05-03 08:34:06

标签: iphone swift uibarbuttonitem

问题在于: enter image description here

当用户点击星形图标时,其垂直位置会改变6个点(我已经跟踪了它的帧)。原因是我使用UIBarButtonItem的自定义字体:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIView.self]).setTitleTextAttributes([NSFontAttributeName : universalSmallFont], for: .normal)

但我不知道如何防止这种框架改变。我尝试在图像更改之前获取按钮的帧并在更改图像后再次应用它,但它不起作用:

func setBarButtonImage(button: UIBarButtonItem, imageName: FontAwesome) {
        var _frame:CGRect?
        let barButtonItem = self.navigationItem.rightBarButtonItem!
        let buttonItemView = barButtonItem.value(forKey: "view")
        if let frame = (buttonItemView as? UIView)?.frame {
            _frame = frame
        }

        let icon = UIImage.fontAwesomeIcon(name: imageName, textColor: UIColor.black, size: CGSize(width: 30, height: 30))

        button.image = icon
        if _frame != nil {
            (barButtonItem.value(forKey: "view") as? UIView)?.frame = _frame!
        }

}

0 个答案:

没有答案