我有按钮。为了获得按钮的标题,我向服务器发送请求并设置Button的标题。现在我需要设置自动调整大小这个按钮。这个怎么做 ?我在按钮的身份检查器中将LineWrap设置为LineWrap。现在文本显示为多行,但我也需要调整按钮的大小,因为一些文本太大而且太小。这是它在故事板中的样子,约束和约束都可以,我只需要调整按钮大小。 如何根据标题的按钮将autoSize设置为UIbutton?
import UIKit
@IBDesignable
class ButtonTableViewCell: UITableViewCell {
@IBInspectable var selectedColor : UIColor = UIColor.init(red: 34/255, green: 89/255, blue: 128/255, alpha: 1.0)
@IBInspectable var normalColor : UIColor = UIColor.init(red: 59/255, green: 169/255, blue: 246/255, alpha: 1.0)
@IBOutlet weak var variant4button: UIButton!
@IBOutlet weak var variant3button: UIButton!
@IBOutlet weak var variant2Button: UIButton!
@IBOutlet weak var variant1button: UIButton!
这里是4个按钮。 这里我设置标题:
if (numberOfVariants.count != 0) {
let questionTextView = cell.contentView.viewWithTag(5) as! UITextView
questionTextView.text = "\(Questions[indexPath.row].content!)"
variant1.addTarget(self, action: #selector(self.variant1ButtonPressed), for: .touchUpInside)
variant2.addTarget(self, action: #selector(self.variant2ButtonPressed), for: .touchUpInside)
variant3.addTarget(self, action: #selector(self.variant3ButtonPressed), for: .touchUpInside)
variant4.addTarget(self, action: #selector(self.variant4ButtonPressed), for: .touchUpInside)
let numberOfVars = numberOfVariants[indexPath.row]
if (numberOfVars == 2) {
variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
variant3.isHidden = true
variant4.isHidden = true
}
else if (numberOfVars == 3){
variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal)
variant4.isHidden = true
}
else if (numberOfVars == 4) {
variant1.setTitle(Variants[0+amaunty[indexPath.row]].title, for: .normal)
variant2.setTitle(Variants[1+amaunty[indexPath.row]].title, for: .normal)
variant3.setTitle(Variants[2+amaunty[indexPath.row]].title, for: .normal)
variant4.setTitle(Variants[3+amaunty[indexPath.row]].title, for: .normal)
}
}
它不工作,我添加了这个代码,但一切都是一样的。请结帐我已将照片添加到问题 –
@АзаматБулегенов对我来说,完美的工作已分配任何新的约束或忘记将类分配给按钮 –
我认为我的单元格TableViewCell中的问题不允许将大小更改为此按钮。你能给我你的电子邮件吗?我会寄给你,也许你会更好理解? –