Input Accessory View doesn't have a height until it is active

55 Views Asked by At

So I have an inputAccessoryView that doesn't have a height until it is initialised. So until you click on it and begin to type, the text cells are behind the the accessoryView and they cannot be seen, it's only until it becomes active and I begin typing that I can scroll all the way down. Now I know I can change the inset sizes and that will fix it but when I start typing again I now have more scrolling space that I don't want. Super frustrating.

Code below:


    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView?.register(ChatMessageCell.self, forCellWithReuseIdentifier: cellId)
        collectionView?.contentInset = UIEdgeInsets(top: 8, left: 0, bottom:8, right: 0)
        collectionView?.backgroundColor = .moonNavy
        collectionView?.alwaysBounceVertical = true
        collectionView?.keyboardDismissMode = .interactive
        
        loadCurrentUserObject()
        setupKeyboardObserve()
    }



  override var canBecomeFirstResponder: Bool { return true }
    var _inputAccessoryView: UIView!
    let textView = UITextView()

    override var inputAccessoryView: UIView? {

        if ( _inputAccessoryView == nil ){

            _inputAccessoryView = CustomView()
            _inputAccessoryView.backgroundColor = UIColor.moonNavy

            //let textView = UITextView()
            textView.font = UIFont(name: "Arial Rounded MT Bold", size: 16)
            textView.text = "Send a message..."
            textView.textColor = .lightGray
            
            textView.backgroundColor = UIColor.white
            textView.layer.cornerRadius = 6
            //_inputAccessoryView.heightAnchor.constraint(equalToConstant: 90).isActive = true
            //textView.borderStyle = .roundedRect
            
            
            let uploadImageView = UIImageView()
            uploadImageView.image = UIImage(named: "gallery-icon")
            uploadImageView.translatesAutoresizingMaskIntoConstraints = false
            let tap = UITapGestureRecognizer(target: self, action: #selector(handleUploadTap))
            uploadImageView.addGestureRecognizer(tap)
            uploadImageView.isUserInteractionEnabled = true
            _inputAccessoryView.addSubview(uploadImageView)
            

            _inputAccessoryView.addSubview(textView)
            //uploadImageView.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
            uploadImageView.leftAnchor.constraint(equalTo: textView.rightAnchor).isActive = true
            uploadImageView.bottomAnchor.constraint(equalTo: textView.bottomAnchor).isActive = true
            uploadImageView.widthAnchor.constraint(equalToConstant: 50).isActive = true
            //uploadImageView.topAnchor.constraint(equalTo: _inputAccessoryView.topAnchor).isActive = true

            
            _inputAccessoryView.addSubview(sendButton)
            sendButton.rightAnchor.constraint(equalTo: _inputAccessoryView.rightAnchor).isActive = true
            sendButton.bottomAnchor.constraint(equalTo: textView.bottomAnchor).isActive = true
            sendButton.leftAnchor.constraint(equalTo: uploadImageView.rightAnchor).isActive = true
            sendButton.widthAnchor.constraint(equalToConstant:60).isActive = true
            uploadImageView.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true


            _inputAccessoryView.autoresizingMask = .flexibleHeight
            
            textView.translatesAutoresizingMaskIntoConstraints = false

            textView.leadingAnchor.constraint(
                equalTo: _inputAccessoryView.leadingAnchor,
                constant: 10
            ).isActive = true

            textView.trailingAnchor.constraint(
                equalTo: uploadImageView.leadingAnchor,
                constant: -10
            ).isActive = true

            textView.topAnchor.constraint(
                equalTo: _inputAccessoryView.topAnchor,
                constant: 10
            ).isActive = true

            
            // this is the important part :
            textView.bottomAnchor.constraint(
                equalTo: _inputAccessoryView.layoutMarginsGuide.bottomAnchor,
                constant: -10
            ).isActive = true
            textView.delegate = self
            textView.isScrollEnabled = false
            
            textViewDidChange(textView)
        }

        return _inputAccessoryView
    }






class CustomView: UIView {
    override var intrinsicContentSize: CGSize {
        return CGSize.zero
    }
}

0

There are 0 best solutions below