MessageKit / MessageKit

A community-driven replacement for JSQMessagesViewController
https://messagekit.github.io
MIT License
5.99k stars 1.17k forks source link

topStackView's height not becoming 0 after using Attachment Manager #1461

Closed mmdock closed 3 years ago

mmdock commented 3 years ago

Describe the bug I am sending a image and loading the image into the attachment manager before sending. After it is sent, the InputBarAccessoryView is still quite a bit taller.

To Reproduce Steps/code to reproduce the behavior:

DemoCode: For complete demo, see patch file here: file.txt (This is actually a git .patch but github doesn't support adding a .patch file. So rename to file.patch and apply to MessageKit/master. I wrote this patch off of commit ae53cdd if that also helps)

General overview: Manage attachment manager as such:

    func attachmentManager(_ manager: AttachmentManager, shouldBecomeVisible: Bool) {
        setAttachmentManager(active: shouldBecomeVisible)
    }

    func setAttachmentManager(active: Bool) {

        let topStackView = messageInputBar.topStackView
        if active && !topStackView.arrangedSubviews.contains(attachmentManager.attachmentView) {
            topStackView.insertArrangedSubview(attachmentManager.attachmentView, at: topStackView.arrangedSubviews.count)
            topStackView.layoutIfNeeded()
        } else if !active && topStackView.arrangedSubviews.contains(attachmentManager.attachmentView) {
            topStackView.removeArrangedSubview(attachmentManager.attachmentView)
            topStackView.layoutIfNeeded()
        }
    }

Make button and apply to left stack view:

        messageInputBar.setLeftStackViewWidthConstant(to: 42, animated: false)
        let addPictureButton = makeButton(named: "ic_camera") {
            self.attachmentManager(self.attachmentManager, didSelectAddAttachmentAt: self.attachmentManager.attachments.count)
        }
        let leftItems = [addPictureButton, .flexibleSpace]
        messageInputBar.setStackViewItems(leftItems, forStack: .left, animated: false)

       private func makeButton(named: String, completion: (()->Void)? = nil) -> InputBarButtonItem {
        return InputBarButtonItem()
            .configure {
                $0.spacing = .fixed(10)
                $0.image = UIImage(named: named)?.withRenderingMode(.alwaysTemplate)
                $0.setSize(CGSize(width: 42, height: 42), animated: false)
                $0.tintColor = UIColor(white: 0.8, alpha: 1)
            }.onSelected {
                $0.tintColor = .primaryColor
            }.onDeselected {
                $0.tintColor = UIColor(white: 0.8, alpha: 1)
            }.onTouchUpInside { _ in
                print("Item Tapped")
                if let complete = completion {
                    complete()
                }
        }
    }

Expected behavior I expect the InputBarAccessoryView topStackView to return to a height of 0 after sending image and removing attachmentManager's attachmentView.

Note: This issue only seems to occur if the left stack items are too tall. If I change: $0.setSize(CGSize(width: 42, height: 42), animated: false) to $0.setSize(CGSize(width: 38, height: 38), animated: false) it works as expected. It fails to behave correctly for any values >= 39 for the size.

Screenshots Simulator Screen Shot - iPhone 8 - 2020-09-14 at 21 13 37

Screen Shot 2020-09-14 at 9 13 28 PM

With the second screenshot, it is hard to tell, but the topStackView ends right below the typing indicator

Environment

Additional context Note: if you use Xcode 12, you will need to update PINRemoteImage to 3.0.1 which I did not include in the patch in order to streamline to prevent the PINRemoteImage Xcode 12 compilation issue. As such, someone should update to the example to use that asap (it's in their master branch but they haven't yet updated Cocoapods.)

martinpucik commented 3 years ago

Hey @mmdock thank you for raising this issue. In my project after sending a message all I'm calling is invalidatePlugins() and invalidateIntrinsicContentSize() on messageInputBar: InputBarAccessoryView. Could you try to call this to fix your issue? Attachments container sizing is working properly for me with these methods

mmdock commented 3 years ago

@martinpucik will give it a look today. I know I am calling invalidatePlugins already, but not sure about InvalidateIntrinsicContentSize().

mmdock commented 3 years ago

@martinpucik forgot to mention, adding invalidateIntrinsicContentSize() to the end of my setAttachmentManager(active:) function was what was missing. thanks for the help!

aidosk21 commented 1 year ago

thank you! calling invalidateIntrinsicContentSize() also helped for me!