2016-11-19 55 views
1

我的目標是以編程方式創建多個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 

仍然得到同樣的錯誤。

+0

哪種方法是崩潰的代碼?你確定在調用viewDidLoad之後調用它嗎? – rmaddy

+0

請更新代碼片段,而不是屏幕截圖。 – iDeveloper

回答

1

您的代碼問題在於,在您調用發生崩潰的方法後,您正在初始化您的scrollView,因爲那時您的scrollView爲零。

你需要從這裏

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 

view.addSubview(scrollView) 

編輯

override func viewDidLoad() { 
    super.viewDidLoad() 

    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) 

    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 
} 

還打電話給你的方法刪除此

self.view.addSubview(scrollView) 
+0

對於錯誤類型您是正確的。我按照您的建議代碼更新了問題,並在上​​面發佈了它。仍然沒有運氣。 –

+0

我嘗試了你的建議,但仍然收到同樣的錯誤。我明白你在做什麼,並同意這是一條路。我認爲我只是要廢棄和重寫。我很想聽聽你有沒有其他建議。謝謝 –

相關問題