有一个添加到子视图的按钮。该按钮的文字大小可以变化。这是按钮代码:
let button: LinkButton = {
let bt = LinkButton()
bt.LinkString = link
bt.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
bt.setTitle("TEXT", for: UIControlState.normal)
bt.titleLabel?.numberOfLines = 0
bt.contentHorizontalAlignment = .left
bt.setTitleColor(UIColor(red: 0.07, green: 0.32, blue: 0.89, alpha: 1.0), for: UIControlState.normal)
bt.addTarget(self, action: #selector(linkOut), for: .touchUpInside)
return bt
}()
问题是,如果文本太大,按钮的大小不会调整为文本的大小,而是开始运行到其他子视图中:
指定约束,并在必要时定位按钮。问题只在于尺寸。
按钮的约束:
contentView.addSubview(button)
button.anchor(bottomAnchor, left: self.leftAnchor, bottom: nil, right: self.rightAnchor, topConstant: 20, leftConstant: 15, bottomConstant: 0, rightConstant: 15, widthConstant: 0, heightConstant: 0)
在bottomAnchor中,前一个元素的bottomAnchor存储在子视图输出循环
中