【问题标题】:Custom button as leftBarButtonItem自定义按钮为 leftBarButtonItem
【发布时间】:2018-07-21 16:34:50
【问题描述】:

我正在使用此代码将自定义按钮显示为 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,我的问题是如何在不裁剪的情况下将文本与图像分开? 提前致谢!

【问题讨论】:

  • 设置边缘插入后,您是否尝试调用sizeToFit
  • @rmaddy 是的,它不能解决问题
  • 尝试使用 size 来适应 button.titleLabel 上的属性并设置 minimumfontsize
  • @shubham 不,它不起作用
  • 需要设置按钮的边框

标签: ios swift uinavigationitem


【解决方案1】:

这里是修复:

 button.titleLabel?.adjustsFontSizeToFitWidth = true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 2017-11-14
    • 2016-08-08
    • 2013-09-20
    相关资源
    最近更新 更多