Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't self.navigationController?.pushViewController my MessagesViewController, blank screen #1071

Closed
jamessuske1985 opened this issue May 14, 2019 · 26 comments
Labels

Comments

@jamessuske1985
Copy link

Hi All,

I am trying to push my MessagesViewController, but when I do, I get a blank screen. I am able to present the view controller, but not push. What am I doing wrong?

let viewController = storyboard?.instantiateViewController(withIdentifier: "ChatController") as! ChatController
            
            //present(viewController, animated: true, completion: nil)
            
            self.navigationController?.pushViewController(viewController, animated: true)
@nathantannar4
Copy link
Member

nathantannar4 commented May 14, 2019

Make sure if you have overriden any functions that you called super within them

Sent with GitHawk

@jamessuske1985
Copy link
Author

Here is my whole controller:

import MessageKit
import MessageInputBar

class ChatController: MessagesViewController, MessagesDataSource, MessagesLayoutDelegate, MessagesDisplayDelegate, MessageInputBarDelegate
{
    
    var messages: [MessageClass] = []
    var member: Member!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let firstname = UserDefaults.standard.string(forKey: "firstname")
        
        let lastname = UserDefaults.standard.string(forKey: "lastname")
        
        member = Member(name: firstname! + " " + lastname!)
        messagesCollectionView.messagesDataSource = self
        messagesCollectionView.messagesLayoutDelegate = self
        messageInputBar.delegate = self
        messagesCollectionView.messagesDisplayDelegate = self
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func numberOfSections(
        in messagesCollectionView: MessagesCollectionView) -> Int {
        return messages.count
    }
    
    func currentSender() -> Sender {
        return Sender(id: member.name, displayName: member.name)
    }
    
    func messageForItem(
        at indexPath: IndexPath,
        in messagesCollectionView: MessagesCollectionView) -> MessageType {
        
        return messages[indexPath.section] as MessageType
    }
    
    func messageTopLabelHeight(
        for message: MessageType,
        at indexPath: IndexPath,
        in messagesCollectionView: MessagesCollectionView) -> CGFloat {
        
        return 12
    }
    
    func messageTopLabelAttributedText(
        for message: MessageType,
        at indexPath: IndexPath) -> NSAttributedString? {
        
        return NSAttributedString(
            string: message.sender.displayName,
            attributes: [.font: UIFont.systemFont(ofSize: 12)])
    }
    
    func heightForLocation(message: MessageType,
                           at indexPath: IndexPath,
                           with maxWidth: CGFloat,
                           in messagesCollectionView: MessagesCollectionView) -> CGFloat {
        
        return 0
    }
    
    func configureAvatarView(
        _ avatarView: AvatarView,
        for message: MessageType,
        at indexPath: IndexPath,
        in messagesCollectionView: MessagesCollectionView) {
        
    }
    
    func messageInputBar(
        _ inputBar: MessageInputBar,
        didPressSendButtonWith text: String) {
        
        let newMessage = MessageClass(
            member: member,
            text: text,
            messageId: UUID().uuidString)
        
        messages.append(newMessage)
        inputBar.inputTextView.text = ""
        messagesCollectionView.reloadData()
        messagesCollectionView.scrollToBottom(animated: true)
    }
    
}

@jamessuske1985
Copy link
Author

Still having the same issue, been stuck on this for days.

@nathantannar4
Copy link
Member

I'm sorry there is little I can help with since the problem is probably in your storyboard file. Is the identifier correct? Did you reference the correct storyboard?

It is my recommendation that you do not use a storyboard. Just create an instance of ChatController and push it

Sent with GitHawk

@jamessuske1985
Copy link
Author

jamessuske1985 commented May 14, 2019 via email

@nathantannar4
Copy link
Member

let vc = ChatController()

Sent with GitHawk

@jamessuske1985
Copy link
Author

jamessuske1985 commented May 14, 2019

I tried this and deleted the scene from the storyboard:

let viewController = ChatController()
            
self.navigationController?.pushViewController(viewController, animated: true)

And it still did not work :(

@nathantannar4
Copy link
Member

nathantannar4 commented May 14, 2019

Ok, can you please define what you mean by blank screen? Maybe attach a gif? Please explain what you have tried to do to fix this. I can not help if you do not give useful debugging information.

This isn't reproducible in the example so you probably have an error somewhere. I see in your code that you never fetch messages, so again what do you mean by blank screen when you are not even loading messages.

@jamessuske1985
Copy link
Author

What I mean by blank screen is that nothing appears, not even the chat bar at the bottom, when I present the ChatController the bar at the bottom appears.

@jamessuske1985
Copy link
Author

ill run it again and see if anything comes up in my log.

@jamessuske1985
Copy link
Author

Nothing appears in my log.

@ZaneH
Copy link

ZaneH commented May 14, 2019

Put a breakpoint in viewDidLoad

Does it ever hit?

@jamessuske1985
Copy link
Author

Yes, it hits the break point in viewDidLoad

@jamessuske1985
Copy link
Author

May-14-2019 21-46-58

@nathantannar4
Copy link
Member

nathantannar4 commented May 15, 2019

You are using a custom view controller. Thus you have not properly allowed the ChatController to be the first responder, so the input bar never appears. There is no messages because you did not load any.

This is all something I could have quite quickly pointed out early had you been a bit more proactive with the problem description. You ignored the issue template where we ask for such info.

Please look at other issues people discuss the input bar not appearing and/or see the embedded example in the ChatExample.

Sent with GitHawk

@jamessuske1985
Copy link
Author

jamessuske1985 commented May 15, 2019

Where exactly is the example in the ChatExample?

@nathantannar4
Copy link
Member

It's called embedded. But you may more likely need to correct allow canBecomeFirstResponder from your custom container controller

Sent with GitHawk

@jamessuske1985
Copy link
Author

Okay, I created this view controller with no storyboard scene

import UIKit

class ChatViewController: UIViewController {
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    let bannerView: UIView = {
        let view = UIView()
        view.backgroundColor = .black
        view.alpha = 0.7
        return view
    }()
    
    let chatController = ChatController()
    
    override var canBecomeFirstResponder: Bool {
        return chatController.canBecomeFirstResponder
    }
    
    override var inputAccessoryView: UIView? {
        return chatController.inputAccessoryView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        chatController.willMove(toParent: self)
        addChild(chatController)
        view.addSubview(chatController.view)
        chatController.didMove(toParent: self)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.isTranslucent = true
        navigationController?.navigationBar.barTintColor = .clear
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.navigationBar.isTranslucent = false
        navigationController?.navigationBar.barTintColor = .black
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let headerHeight: CGFloat = 200
        bannerView.frame = CGRect(origin: .zero, size: CGSize(width: view.bounds.width, height: headerHeight))
        chatController.view.frame = CGRect(x: 0, y: headerHeight, width: view.bounds.width, height: view.bounds.height - headerHeight)
    }
    
}

and I am doing my push like so:

self.navigationController?.pushViewController(ChatViewController(), animated: true)

but now I am getting this blank screen with no tool bar.
Screen Shot 2019-05-16 at 12 39 47 AM

@nathantannar4
Copy link
Member

But where is your ChatViewController held? Before you saw you had some custom drawer controller. I think it's the reason the input bar is not appearing because it doesn't allow your ChatViewController to be the first responder

Sent with GitHawk

@jamessuske1985
Copy link
Author

jamessuske1985 commented May 16, 2019

My bad, I named my View Controller to ChatViewController...bad name, I changed the name to ChatContainerController but I still have the same issue. ChatContainerController is like your MessageContainerController and my ChatController is a MessagesViewController

@jamessuske1985
Copy link
Author

I did notice that return chatController.canBecomeFirstResponder is not being called

@jamessuske1985
Copy link
Author

I have break points in both controllers and they are both being called.

@jamessuske1985
Copy link
Author

Any additional information you need? chatController.canBecomeFirstResponder is not being called

@nathantannar4
Copy link
Member

Again I will state the issue is you custom drawer container controller. If you built it yourself you should review https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

Sent with GitHawk

@stale
Copy link

stale bot commented Jun 2, 2019

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label Jun 2, 2019
@stale
Copy link

stale bot commented Jun 9, 2019

This issue has been auto-closed because there hasn't been any activity for at least 21 days. However, we really appreciate your contribution, so thank you for that! 🙏 Also, feel free to open a new issue if you still experience this problem 👍.

@stale stale bot closed this as completed Jun 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants