以编程方式将UILabel和UIButtons添加到Swift

时间:2016-11-19 05:32:33

标签: ios swift uiscrollview uibutton uilabel

我的目标是以编程方式创建多个UILabel和UIButtons,然后以编程方式创建并将它们插入到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)
}

创建标签和按钮后,我使用以下语句将视图分别添加到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)
        }

我使用类似的语句将所有创建的内容添加到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

仍然遇到同样的错误。

1 个答案:

答案 0 :(得分:2)

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