我有按鈕。爲了獲得按鈕的標題,我向服務器發送請求並設置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中的問題不允許將大小更改爲此按鈕。你能給我你的電子郵件嗎?我會寄給你,也許你會更好理解? –