自定义按钮为leftBarButtonItem

时间:2018-07-21 16:34:50

标签: ios swift uinavigationitem

我正在使用此代码将自定义按钮显示为leftButtonItem:

    let button = UIButton(type: .system)
    button.setTitleColor(.white, for: .normal)
    button.setImage(UIImage(named: "Back"), for: UIControlState.normal)
    button.addTarget(self, action: #selector (DetailExperienceTVC.back), for: .touchUpInside)
    button.setTitle(DataManager.shared.arrayMenuTop[DataManager.shared.indexTitle].title, for: .normal)
    button.titleLabel?.font = ColorManager.shared.generalFont30

    button.sizeToFit()

    button.titleEdgeInsets = UIEdgeInsetsMake((self.navigationController!.navigationBar.frame.height) / 4, 10, 0, 0)

    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)

但这是我得到的wrong size button,我的问题是如何在不裁剪的情况下从图像中区分出文本? 预先感谢!

1 个答案:

答案 0 :(得分:-1)

解决方法:

 button.titleLabel?.adjustsFontSizeToFitWidth = true