【问题标题】:Programmatically adding UILabels and UIButtons to UIScrollView in Swift在 Swift 中以编程方式将 UILabel 和 UIButtons 添加到 UIScrollView
【发布时间】:2017-04-03 01:00:35
【问题描述】:

我的目标是以编程方式创建多个 UILabel 和 UIButton,然后以编程方式创建并将它们插入 UIScrollView。

我在以下班级工作。

class ViewController: UIViewController {

我首先创建了如下所示的滚动视图。

    @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).

这里有一些 sn-ps 展示了我如何创建我的 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 之后调用的吗?
  • 请更新代码 sn-p,不是屏幕截图。

标签: ios swift uiscrollview uibutton uilabel


【解决方案1】:

您的代码的问题是您在调用导致崩溃的方法之后正在初始化您的scrollView,因为当时您的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

删除这个

    self.view.addSubview(scrollView)

【讨论】:

  • 关于错误类型,您是正确的。我用您建议的代码更新了问题并将其发布在上面。还是没有运气。
  • 我尝试了您的建议,但仍然遇到同样的错误。我看到你在做什么,并同意这是要走的路。我想我只是要废弃和重写。如果您有任何其他建议,我很想听听。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
相关资源
最近更新 更多