我的目標是以編程方式創建多個UILabels和UIButton,然後以編程方式創建並將它們插入到UIScrollView中。以編程方式將UILabels和UIButtons添加到Swift中的UIScrollView
我在以下班級內工作。
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)
。
這裏有一些片段顯示了我如何創建我的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)
}
我使用了類似的語句來添加所有創建的內容的滾動型。我已經在堆棧中的其他解決方案以及其他在線參考之後對我的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