以编程方式将UILabels和UIButtons添加到Swift中的UIScrollView

问题描述:

我的目标是以编程方式创建多个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

您的代码问题在于,在您调用发生崩溃的方法后,您正在初始化您的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

我尝试了你的建议,但仍然收到同样的错误。我明白你在做什么,并同意这是一条路。我认为我只是要废弃和重写。我很想听听你有没有其他建议。谢谢 –