Search code examples
swiftuicollectionviewuitoolbar

UIToolBar with UICollectionView


I have a UIToolbar view that has a UICollectionView in it.

The toolbar is a text view with a collectionview where the collectionview is filled with user mentions. So if in the textview I type "@m" it will display usernames that begin to match with the letter "m".

It used to not be in the toolbar, but we realized it didn't dismiss properly with interactive dismisses of the keyboard and adding it to the toolbar fixed that. (It would hover in the middle of the screen during the interactive dismiss and wouldn't go away)

However now all user interaction doesn't work on it anymore (Despite it being enabled in IB)

Here is the set up of the toolbar:

override var canBecomeFirstResponder: Bool{
    return true
}

override var inputAccessoryView: UIView?{
    return self.typingView
}

//Inside viewDidLoad:

    let separator = UIView(frame: CGRect(x:0 , y: 0, width: ScreenSize.width(), height: 1))
    separator.backgroundColor = UIColor.lightBackgroundGrey
    self.typingView.addSubview(separator)
    self.typingView.isTranslucent = false
    self.typingView.setShadowImage(UIImage(), forToolbarPosition: .any)
    self.typingView.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
    self.typingView.removeFromSuperview()
    self.setupMentionableCollectionView() //Sets delegate and data source only

Solution

  • Since you created the toolbar in IB, then you probably have height constraints on the toolbar and/or collectionView, so you should update those whenever the contentSize of the collectionView changes.

    I think what's going on is that the frame of the collectionView (which is a subview of the toolbar) is larger than the frame of the toolbar, so that's why you can't click it.

    Just do self.collectionViewHeightConstraint.constant = whatever and self.typingViewHeightConstraint.constant = whatever + defaultHeight and that should solve your problem.