首页 > 解决方案 > UIButton 在 iOS 15 上总是截断文本

问题描述

更新到 xcode 13 / ios 15 后。我明白了,UIButton 上的文本总是被截断,大小不固定。文本从 PushRelay (RxSwift) 传递。

请参阅水平 UIStackView 上的 UIButton。

代码如下

  let tagButton = UIButton()
  addSubview(tagButton)
  tagButton.layer.cornerRadius = 16
  tagButton.layer.masksToBounds = true
  tagButton.titleEdgeInsets = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
  tagButton.snp.makeConstraints({
    $0.edges.equalToSuperview()
  })

文本由以下人员提供:

  bag.insert(
    viewModel.outputs.titleAttrText
      .drive(tagButton.rx.attributedTitle(for: .normal))
  )
  

标签: uibuttonrx-swifttruncatedios15

解决方案


我在 UIStackView 中看到了这个问题。

从 titleEdgeInsets 切换到 contentEdgeInsets 为我解决了这个问题。


推荐阅读