私の目標は、プログラムで複数のUILabelsとUIButtonsを作成し、プログラムで作成してUIScrollViewに挿入することです。SwiftのUIScrollViewにUILabelsとUIButtonsをプログラムで追加する
私は以下のクラスで動作しています。
class ViewController: UIViewController {
私は最初に以下のように私のscrollViewを作成しました。
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
createTitleBar() //Function that creates fixed buttons and labels not included in scrollView
createUpperTabs() //Function that creates fixed buttons and labels not included in scrollView
createLowerNavBars() //Function that creates fixed buttons and labels not included in scrollView
for _ in 0...10 {
createTemplate() //This function consists of nested functions that creates labels and buttons that are then added to the scrollView
}
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.black
scrollView.contentSize = CGSize(width: screenSize.width, height: currentStackY)
scrollView.autoresizingMask = UIViewAutoresizing.flexibleHeight
view.addSubview(scrollView)
}
は、ラベルやボタンを作成した後、私は声明 self.scrollView.addSubview(UILabel or UIButton Name)
を以下にscrollViewに個別にビューを追加します。
UILabelとUIButtonの作成方法を示す抜粋です。
let titleHeader = UILabel()
self.scrollView.addSubview(titleHeader)
currentStackY += 8
titleHeader.backgroundColor = .gray
titleHeader.text = headerTitle
titleHeader.font = UIFont(name: buttonFont, size: buttonFontSize)
let labelWidth = titleHeader.intrinsicContentSize.width + 16
titleHeader.snp.makeConstraints { (make) in
make.height.equalTo(titleHeader)
make.width.equalTo(titleHeader)
make.top.equalTo(currentStackY)
make.left.equalTo(view).offset(8)
}
など
for i in 0...chiefComplaintArray.count - 1 {
let btn = UIButton()
chiefComplaintButton.append(btn)
buttonDictionary[chiefComplaintButton[i]] = numberButtonPressed
chiefComplaintButton[i].setTitle(chiefComplaintArray[i], for: .normal)
chiefComplaintButton[i].setTitleColor(UIColor.black, for: .normal)
chiefComplaintButton[i].titleLabel!.font = UIFont(name: buttonFont, size: buttonFontSize)
chiefComplaintButton[i].backgroundColor = UIColor.gray
chiefComplaintButton[i].sizeToFit()
let buttonWidth = chiefComplaintButton[i].frame.width + 10
let buttonHeight = chiefComplaintButton[i].frame.height + 5
if i == 0 {
currentStackX = 20
currentStackY += 5
} else if (currentStackX + buttonWidth) > screenSize.width {
currentStackX = 20
currentStackY += buttonHeight + 5
}
chiefComplaintButton[i].frame = CGRect(x: currentStackX, y: currentStackY, width: buttonWidth, height: buttonHeight)
currentStackX += chiefComplaintButton[i].frame.width + 5
if i == chiefComplaintArray.count - 1 {
currentStackY += chiefComplaintButton[i].frame.height
}
self.scrollView.addSubview(chiefComplaintButton[i])
chiefComplaintButton[i].addTarget(self, action: #selector(self.buttonPressed), for: .touchUpInside)
}
私はscrollViewに作成されたすべてのコンテンツを追加するために同様のステートメントを使用していました。スタック内の他のソリューションや他のオンラインリファレンスの後に私のUIScrollViewをモデル化しました。あなたの時間と配慮に事前に感謝します。
私はその後、まだ同じエラーを取得し
let titleHeader = UILabel()
scrollView.addSubview(titleHeader)
self.view.addSubview(scrollView)
currentStackY += 8
titleHeader.backgroundColor = .gray
titleHeader.text = headerTitle
titleHeader.font = UIFont(name: buttonFont, size: buttonFontSize)
let labelWidth = titleHeader.intrinsicContentSize.width + 16
を試してみました。
クラッシュするコードはどれですか? 'viewDidLoad'が呼ばれた後に呼び出されているのでしょうか? – rmaddy
スクリーンショットではなく、コードスニペットを更新してください。 – iDeveloper