集合视图中的自定义UITextField未显示

时间:2016-12-21 19:06:39

标签: ios swift autolayout

将此自定义TexView添加到我的集合视图时遇到问题。我可以添加颜色和控制台打印输出(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")
}

在我的viewDidLoad中用于UICollectionView

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但是当我取消注释backgroundColor时可以看到颜色。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的约束需要明确指定视图的位置和大小(即解决方案必须是一组具体值)。您可以通过允许自动布局根据视图的内容大小(设置translatesAutoresizingMaskIntoConstraintstrue)或通过自己指定具体值来使用隐式约束来实现此目的(使用&#34;等于&#34 ;约束而不是&#34;小于或等于&#34;)。

  

我通过使用标签而不是textView

暂时解决了问题

UITextView是一个滚动容器(它是UIScrollView的子类),因此UILabel没有隐式内容大小。

相关问题