将此自定义 TexView 添加到我的 Collection View 中时遇到问题。我可以添加颜色,控制台会打印出来(99999999),但我的自定义 UItextview 不会显示。
class chatMessageCell: UICollectionViewCell {
let textView1: UITextView = {
let tV = UITextView()
tV.text = "sample text"
tV.font = UIFont.systemFontOfSize(18)
tV.translatesAutoresizingMaskIntoConstraints = false
return tV
}()
override init(frame: CGRect) {
super.init(frame: frame)
//Uncommentting this shows RED COLOR for each collectionView
//backgroundColor = UIColor.redColor()
addSubview(textView1) //<----for some reason this is not showing
textView1.centerYAnchor.constraintLessThanOrEqualToAnchor(self.centerYAnchor).active = true
textView1.centerXAnchor.constraintLessThanOrEqualToAnchor(self.centerXAnchor).active = true
textView1.heightAnchor.constraintLessThanOrEqualToAnchor(self.heightAnchor).active = true
textView1.widthAnchor.constraintLessThanOrEqualToConstant(200).active = true
print(9999999999999999) <----this does print confirming that block works
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
在我看来,UICollectionView 的DidLoad
class ChatLogController: UICollectionViewController, UITextFieldDelegate, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.whiteColor()
collectionView!.registerClass(chatMessageCell.self, forCellWithReuseIdentifier: cellId)
setupInPutComponent()
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messageS.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath)
return cell
}
//make collectionview horizontal like TableView
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 80)
}
....
我没有收到任何错误,并且代码有意义。不知道为什么我看不到 UITextview,但当我取消注释背景颜色时可以看到颜色。提前致谢。
最佳答案
您的约束需要明确指定 View 的位置和大小(即解决方案必须是一组具体值)。您可以通过允许自动布局使用基于 View 内容大小的隐式约束(将 translatesAutoresizingMaskIntoConstraints
设置为 true
)或自行指定具体值(使用“相等”约束来实现此目的) “小于或等于”)。
I solved the problem temporarily by using a label instead of a textView
UITextView
是一个滚动容器(它是 UIScrollView
的子类),因此它没有隐式内容大小,而 UILabel
有。
关于ios - Collection View 中的自定义 UITextField 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41270161/