我正在尝试在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)
}
}