ios - 在 Swift 中以编程方式将 UILabels 和 UIButtons 添加到 UIScrollView

标签 ios swift uiscrollview uibutton uilabel

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

我在以下类(class)中操作。

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)
}

创建标签和按钮后,我使用以下语句将 View 单独添加到 ScrollView 中 self.scrollView.addSubview(UILabel或UIButton名称)

这里有一些片段展示了我如何创建 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

仍然遇到同样的错误。

最佳答案

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

关于ios - 在 Swift 中以编程方式将 UILabels 和 UIButtons 添加到 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40689574/

相关文章:

iOS Storyboard 可本地化字符串不适用于 UILabel 子类

iphone - 没有任何谷歌结果的神秘警告 [在模拟器中]

ios - 关于在 Swift 中均衡因设备而异的 collectionView 单元格大小的问题

ios - 更改框架后 UIContainerView 停止接收触摸

ios - 滑动以导航到另一个 ViewController 并根据手指移动进行更改

ios - 使用按钮将 childViewController 加载到 scrollview parent

ios - 纯代码关于UICollectionView水平分页

ios - 在 Swift map 上显示路线

ios - 如何在更改 View 的同时仍然让用户在 TextField 中进行编辑?

ios - 通过 UIScrollView 使底层 View 可点击