2016-11-19 6 views
1

私の目標は、プログラムで複数の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 

を試してみました。

+0

クラッシュするコードはどれですか? 'viewDidLoad'が呼ばれた後に呼び出されているのでしょうか? – rmaddy

+0

スクリーンショットではなく、コードスニペットを更新してください。 – iDeveloper

答えて

1

コードで問題が発生したのは、scrollViewがnilであるため、クラッシュするメソッドを呼び出した後にscrollViewを初期化するということです。

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 
} 

また、ここで

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

エラーの種類に関して正しいです。あなたが提案し、上に掲示したコードで質問で更新しました。まだ運がありません。 –

+0

私はあなたの提案を試みたが、同じエラーが発生しています。私はあなたがしていることを見て、それが行く方法であることに同意する。私はちょうどスクラップして書き直すつもりだと思う。私は他の提案があればお聞きしたいと思います。ありがとう –

関連する問題