0

我在 tableViewCell 中有一个按钮,但我无法缩小它以适应它的 titleLabel,它比按钮小。该按钮似乎有一个最小框架。当我删除单元格内的所有约束时,我可以再次更改其大小。但是我应该如何在有这些限制的同时缩小它呢?

按钮 - “subjectButton” - 在单元格中

在此处输入图像描述

“subjectButton”的约束:

在此处输入图像描述

我的尝试:

func setButtonStyle(_ button: UIButton, subject: String) {
    button.backgroundColor = UIColor.lightGray
    button.setTitle(subject, for: .normal)
    button.setTitleColor(UIColor.white, for: .normal)
    button.layer.cornerRadius = 3
    button.titleLabel?.sizeToFit()
    button.sizeToFit()
    button.frame.size = CGSize(width: 5, height: 20)
}

模拟器(打开“彩色混合图层”):

在此处输入图像描述

如您所见,使用button.sizeToFit. 此外,我未能使用button.frame.size = CGSize(width: 5, height: 20).

为什么?提前致谢。

4

2 回答 2

0

解决了。

button.widthAnchor.constraint(equalToConstant: button.titleLabel?.frame.width ?? 0).isActive = true
于 2018-12-31T06:19:16.347 回答
0

创建你的 Constaint @property IBOutlet Like

@property (nonatomic,strong) IBOutlet NSLayoutConstraint *height;

改变那里的高度后

self.height.constant = button.frame.size.height + 15;
于 2018-12-17T13:22:03.967 回答