如何在视图控制器中访问uicollectionviewcell的内容?

我正在尝试在UICollectionViewCell内的文本字段中添加底部边框,我将单元格注册在我的集合视图所在的视图控制器内。但是要设置底部边框的大小,我需要设置其自身的大小,并且我不知道如何在集合视图单元格中进行操作,因此我试图将其传递给注册了它的视图控制器,但是没有成功了。

*糟糕:由于不相关,我删去了部分代码。

UICollectionViewCell

class NameStepCell: UICollectionViewCell {

    let safeAreaHolder: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskintoConstraints = false
        return view
    }()

    let title: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskintoConstraints = false
        label.numberOfLines = 0
        label.font = UIFont.boldSystemFont(ofSize: 40)
        label.text = "What is\nyour\nname?"
        return label
    }()

    let txtFieldStack: UIStackView = {
        let stack = UIStackView()
        stack.translatesAutoresizingMaskintoConstraints = false
        stack.alignment = .center
        stack.axis = .horizontal
        stack.distribution = .fillEqually
        stack.spacing = 20
        return stack
    }()

    let nameField: UITextField = {
        let txtFld = UITextField()
        txtFld.keyboardType = UIKeyboardType.default
        txtFld.textContentType = UITextContentType.name
        txtFld.autocapitalizationType = UITextAutocapitalizationType.words
        txtFld.autocorrectionType = .no
        txtFld.textColor = UIColor.black
        return txtFld
    }()

    let lastNameField: UITextField = {
        let txtFld = UITextField()
        txtFld.keyboardType = UIKeyboardType.default
        txtFld.textContentType = UITextContentType.familyName
        txtFld.autocapitalizationType = UITextAutocapitalizationType.words
        txtFld.autocorrectionType = .no
        txtFld.textColor = UIColor.black
        return txtFld
    }()




    override init(frame: CGRect) {
        super.init(frame: frame)

        configuringView()
        configuringTitle()
        configuringTxtField()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configuringView(){
        addSubview(safeAreaHolder)
        safeAreaHolder.topAnchor.constraint(equalTo: topAnchor).isactive = true
        safeAreaHolder.trailingAnchor.constraint(equalTo: trailingAnchor,constant: -16).isactive = true
        safeAreaHolder.bottomAnchor.constraint(equalTo: bottomAnchor).isactive = true
        safeAreaHolder.leadingAnchor.constraint(equalTo: leadingAnchor,constant: 16).isactive = true
    }

    func configuringTitle(){
        safeAreaHolder.addSubview(title)

        title.topAnchor.constraint(equalTo: safeAreaHolder.topAnchor,constant: 50).isactive = true
        title.trailingAnchor.constraint(equalTo: safeAreaHolder.trailingAnchor).isactive = true
        title.leadingAnchor.constraint(equalTo: safeAreaHolder.leadingAnchor).isactive = true
    }

    func configuringTxtField(){
        safeAreaHolder.addSubview(txtFieldStack)

        txtFieldStack.topAnchor.constraint(equalTo: title.bottomAnchor,constant: 50).isactive = true
        txtFieldStack.trailingAnchor.constraint(equalTo: safeAreaHolder.trailingAnchor).isactive = true
        txtFieldStack.leadingAnchor.constraint(equalTo: safeAreaHolder.leadingAnchor).isactive = true


        txtFieldStack.addArrangedSubview(nameField)
        txtFieldStack.addArrangedSubview(lastNameField)


        nameField.heightAnchor.constraint(equalToConstant: 45).isactive = true
        lastNameField.heightAnchor.constraint(equalToConstant: 45).isactive = true

    }
}

UIViewController

class SignupViewController: UIViewController,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource{


    let stepsCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal

        let collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
        collectionView.translatesAutoresizingMaskintoConstraints = false
        collectionView.backgroundColor = .white
        collectionView.contentInsetadjustmentBehavior = UIScrollView.ContentInsetadjustmentBehavior.never
        collectionView.isPagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isScrollEnabled = false
        return collectionView
    }()


    override func viewDidLoad() {
        super.viewDidLoad()

        stepsCollectionView.dataSource = self
        stepsCollectionView.delegate = self

        stepsCollectionView.register(NameStepCell.self,forCellWithReuseIdentifier: "nameStepId")
        stepsCollectionView.register(GenderStepCell.self,forCellWithReuseIdentifier: "genderStepId")
        stepsCollectionView.register(BirthdayStepCell.self,forCellWithReuseIdentifier: "birthdayStepId")
        stepsCollectionView.register(EmailStepCell.self,forCellWithReuseIdentifier: "emailStepId")
        stepsCollectionView.register(PasswordStepCell.self,forCellWithReuseIdentifier: "passwordStepId")

        view.backgroundColor = .white
        configuringBottomButton()
        configuringStepCollectionView()
    }

    override func viewDidAppear(_ animated: Bool) {

    }


    Here is where I try to get the nameFied to add the border
    override func viewdidlayoutsubviews() {
        NameStepCell().self.nameField.addBottomBorder()
    }

    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if indexPath.item == 1 {
            let genderCell = collectionView.dequeueReusableCell(withReuseIdentifier: "genderStepId",for: indexPath)
            return genderCell
        }else if indexPath.item == 2{
            let birthdayCell = collectionView.dequeueReusableCell(withReuseIdentifier: "birthdayStepId",for: indexPath)
            return birthdayCell
        }else if indexPath.item == 3{
            let emailCell = collectionView.dequeueReusableCell(withReuseIdentifier: "emailStepId",for: indexPath)
            return emailCell
        }else if indexPath.item == 4{
            let passwordCell = collectionView.dequeueReusableCell(withReuseIdentifier: "passwordStepId",for: indexPath)
            return passwordCell
        }

        let nameCell = collectionView.dequeueReusableCell(withReuseIdentifier: "nameStepId",for: indexPath)
        return nameCell
    }

    func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: stepsCollectionView.frame.size.width,height: stepsCollectionView.frame.size.height)
    }
}

扩展到文本字段以添加底部边框

extension UITextField {
    func addBottomBorder() {
        let border = CALayer()
        border.frame = CGRect(x: 0,y: 32,width: self.frame.size.width,height: 1)
        border.cornerRadius = 2
        border.masksToBounds = true
        border.backgroundColor = UIColor.init(red: 112/255,green: 112/255,blue: 112/255,alpha: 1).cgColor
        self.layer.masksToBounds = true
        self.layer.addSublayer(border)
    }
}
wubuwei111 回答:如何在视图控制器中访问uicollectionviewcell的内容?

您可以尝试这样的操作,而不是在 viewDidLayoutSubviews 中调用 addBottomBar()

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.item == 1 {
        let genderCell = collectionView.dequeueReusableCell(withReuseIdentifier: "genderStepId",for: indexPath)
        return genderCell
    }else if indexPath.item == 2{
        let birthdayCell = collectionView.dequeueReusableCell(withReuseIdentifier: "birthdayStepId",for: indexPath)
        return birthdayCell
    }else if indexPath.item == 3{
        let emailCell = collectionView.dequeueReusableCell(withReuseIdentifier: "emailStepId",for: indexPath)
        return emailCell
    }else if indexPath.item == 4{
        let passwordCell = collectionView.dequeueReusableCell(withReuseIdentifier: "passwordStepId",for: indexPath)
        return passwordCell
    }

    // Dequeue your NameStepCell from collection view
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "nameStepId",for: indexPath)
    if let nameCell = cell as? NameStepCell {
        // Add bottom border to it right here and return it
        nameCell.nameField.addBottomBorder()
        return nameCell
    }
    return cell
}

编辑:

现在,您无需在 SignUpViewController 中进行任何更改。请使用以下代码替换您的 NameStepCell 类。

class NameStepCell: UICollectionViewCell {
    var safeAreaHolder: UIView!
    var title: UILabel!
    var txtFieldStack: UIStackView!
    var nameField: UITextField!
    var lastNameField: UITextField!

    override init(frame: CGRect) {
        super.init(frame: frame)

        configuringView()
        configuringTitle()
        configuringTxtField()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

private extension NameStepCell {
    func configuringView(){
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        self.safeAreaHolder = view

        addSubview(safeAreaHolder)
        safeAreaHolder.topAnchor.constraint(equalTo: topAnchor).isActive = true
        safeAreaHolder.trailingAnchor.constraint(equalTo: trailingAnchor,constant: -16).isActive = true
        safeAreaHolder.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        safeAreaHolder.leadingAnchor.constraint(equalTo: leadingAnchor,constant: 16).isActive = true
        self.safeAreaHolder.layoutIfNeeded()
    }

    func configuringTitle(){
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.numberOfLines = 0
        label.font = UIFont.boldSystemFont(ofSize: 40)
        label.text = "What is\nyour\nname?"
        self.title = label

        safeAreaHolder.addSubview(title)
        title.topAnchor.constraint(equalTo: safeAreaHolder.topAnchor,constant: 50).isActive = true
        title.trailingAnchor.constraint(equalTo: safeAreaHolder.trailingAnchor).isActive = true
        title.leadingAnchor.constraint(equalTo: safeAreaHolder.leadingAnchor).isActive = true
        self.title.layoutIfNeeded()
    }

    func configuringTxtField(){
        let stack = UIStackView()
        stack.backgroundColor = .lightGray
        stack.translatesAutoresizingMaskIntoConstraints = false
        stack.alignment = .center
        stack.axis = .horizontal
        stack.distribution = .fillEqually
        stack.spacing = 20
        self.txtFieldStack = stack

        safeAreaHolder.addSubview(txtFieldStack)

        txtFieldStack.topAnchor.constraint(equalTo: title.bottomAnchor,constant: 50).isActive = true
        txtFieldStack.trailingAnchor.constraint(equalTo: safeAreaHolder.trailingAnchor).isActive = true
        txtFieldStack.leadingAnchor.constraint(equalTo: safeAreaHolder.leadingAnchor).isActive = true
        self.txtFieldStack.layoutIfNeeded()

        self.nameField = getTextField(.name)
        self.lastNameField = getTextField(.familyName)

        txtFieldStack.addArrangedSubview(nameField)
        txtFieldStack.addArrangedSubview(lastNameField)

        nameField.heightAnchor.constraint(equalToConstant: 45).isActive = true
        lastNameField.heightAnchor.constraint(equalToConstant: 45).isActive = true

        // After adding constraints,you should call 'layoutIfNeeded()' which recomputes the size and position based on the constraints you've set
        self.nameField.layoutIfNeeded()
        self.lastNameField.layoutIfNeeded()

        self.nameField.addBottomBorder()
        self.lastNameField.addBottomBorder()
    }

    func getTextField(_ textContentType: UITextContentType) -> UITextField {
        let textField = UITextField()
        textField.keyboardType = .default
        textField.textContentType = textContentType
        textField.autocapitalizationType = .words
        textField.autocorrectionType = .no
        textField.textColor = .black
        textField.placeholder = textContentType.rawValue // P.S. Remove placeholder if you don't need. 
        return textField
    }
}
本文链接:https://www.f2er.com/3134010.html

大家都在问