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

How to hide the AvatarView with v1.0.0 #674

Closed
janweinkauff opened this issue May 5, 2018 · 37 comments
Closed

How to hide the AvatarView with v1.0.0 #674

janweinkauff opened this issue May 5, 2018 · 37 comments

Comments

@janweinkauff
Copy link
Contributor

I'm trying to hide the AvatarView with the new changes of 1.0.0-beta.1.

Right now I use avatarView.isHidden = true in MessagesDisplayDelegate.configureAvatarView which only hides the view but will not remove the space of the AvatarView due to the size from outgoingAvatarSize or incomingAvatarSize of MessageSizeCalculator.

Is it possible to change the value of outgoingAvatarSize or incomingAvatarSize?

I couldn't find any delegate callback or entry point for a subclass.

@janweinkauff
Copy link
Contributor Author

janweinkauff commented May 5, 2018

After a short chat in the slack channel I found a solution based on the suggestion from @creyD.

outgoingAvatarSize or incomingAvatarSize are just properties of the MessagesCollectionViewFlowLayout .

if let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout {
  layout.textMessageSizeCalculator.outgoingAvatarSize = .zero
  ...
}

@Faisal0sal
Copy link

Faisal0sal commented May 5, 2018

if let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout {
layout.textMessageSizeCalculator.outgoingAvatarSize = .zero
layout.textMessageSizeCalculator.incomingAvatarSize = .zero
}

doing this in viewdidload ^^ still gave me an avatar on outgoing messages

@janweinkauff
Copy link
Contributor Author

I also did this change in the viewDidLoad method. It works on my side.

  • Did you check if the cast messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout succeeds?
  • Maybe set a breakpoint to MessageSizeCalculator.avatarSize(for message:) to understand from what change the incomingAvatarSize is set?

@Faisal0sal
Copy link

it works now, just moving it to the bottom of my viewDidLoad did it. not sure how this affect it.

@Faisal0sal
Copy link

Actually although this does work on text messages, once you start loading more messages it starts to flounder between having it and removing it. It also appears on media messages.

simulator screen shot - iphone x - 2018-05-05 at 11 41 09

@janweinkauff
Copy link
Contributor Author

janweinkauff commented May 5, 2018

You can also set the avatar size for all other kind of messages. Please have a look into MessagesCollectionViewFlowLayout.

To change the avatar size you do same as desribed but with a different MessageSizeCalculator:

attributedTextMessageSizeCalculator.outgoingAvatarSize = .zero
emojiMessageSizeCalculator.outgoingAvatarSize = .zero
photoMessageSizeCalculator.outgoingAvatarSize = .zero
videoMessageSizeCalculator.outgoingAvatarSize = .zero
locationMessageSizeCalculator.outgoingAvatarSize = .zero

@Faisal0sal
Copy link

It still flounders
simulator screen shot - iphone x - 2018-05-05 at 13 43 26

@SD10
Copy link
Member

SD10 commented May 5, 2018

@Faisal0sal keep in mind cells are reused

Sent with GitHawk

@SD10 SD10 added the question label May 5, 2018
@ljs19923
Copy link

ljs19923 commented May 7, 2018

Do you know why i get this error ?
image

@SD10
Copy link
Member

SD10 commented May 7, 2018

@JulienLevallois I'm not sure exactly. What is your MessageKit version? Have you tried clearing derived data folder?

@ljs19923
Copy link

ljs19923 commented May 7, 2018

My version is 0.13.0 and yes i deleted this folder
image

@ljs19923
Copy link

ljs19923 commented May 7, 2018

I need to update to 0.13.5 or 1.0.0-beta.1 ?

Because with pod 'MessageKit' and pod install i got this version 0.13.0

@SD10

@SD10
Copy link
Member

SD10 commented May 7, 2018

You need 1.0.0-beta.1
Run pod repo update, then pod install

@ljs19923
Copy link

ljs19923 commented May 7, 2018

I got this error :/

[!] Failed to connect to GitHub to update the CocoaPods/Specs specs repo - Please check if you are offline, or that GitHub is down

@Faisal0sal
Copy link

Faisal0sal commented May 7, 2018

pod 'MessageKit', '~> 1.0.0-beta.1'

This is what worked for me.

@ljs19923
Copy link

ljs19923 commented May 7, 2018

How can i verticaly align the avatar bottom with the cell in this new version ? 1.0.0-Beta.1

image

@cwalo
Copy link
Contributor

cwalo commented May 7, 2018

@JulienLevallois Please create a separate issue for your question. Thanks!

@ljs19923
Copy link

ljs19923 commented May 7, 2018

When i send a new message the outgoingAvatarSize = .zero didn't works

image

@jaunesarmiento
Copy link

Tried setting the avatar size using the following but I'm still getting blank spaces in the avatar area.

override func viewDidLoad() {
  super.viewDidLoad()

  // some code here

  if let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout {
    layout.textMessageSizeCalculator.outgoingAvatarSize = .zero
    layout.textMessageSizeCalculator.incomingAvatarSize = .zero
  }
}

simulator screen shot - iphone 8 plus - 2018-05-09 at 13 04 54

What am I missing?

@SD10
Copy link
Member

SD10 commented May 9, 2018

@jaunesarmiento because each case of MessageKind enum has its own CellSizeCalculator object as a property of MessagesCollectionViewFlowLayout. Look at the other properties in the class and set those to zero as well if you'd like

@jaunesarmiento
Copy link

Ah! @SD10 silly me. Thanks for pointing it out.

@ndonald2
Copy link

ndonald2 commented May 9, 2018

@SD10 it would be nice to somehow be able to set avatar sizes for all calculators at once rather than having to set size on each calculator individually, for convenience's sake. Do you think there's a place for that in the 1.0 API?

@SD10
Copy link
Member

SD10 commented May 9, 2018

@ndonald2 Maybe 🤔 The maintenance burden for that would be lightweight but I dislike expanding the API surface area with something a user could do themselves. We would have to also look at a convenience method for the other calculator properties as well.

@ljs19923
Copy link

image

I removed Avater view but how can i align my bottom date to the right ? without avatar.
Thanks

@ndonald2
Copy link

@SD10

I dislike expanding the API surface area with something a user could do themselves

I totally agree, but as you mentioned the maintenance cost for this would be quite low. In my opinion it seems like a very common use case to just want to change MK's default avatar size, which for both outgoing and incoming messages across all message types is currently extremely verbose:

layout.textMessageSizeCalculator.incomingAvatarSize = avatarSize
layout.textMessageSizeCalculator.outgoingAvatarSize = avatarSize
layout.attributedTextMessageSizeCalculator.incomingAvatarSize = avatarSize
layout.attributedTextMessageSizeCalculator.outgoingAvatarSize = avatarSize
layout.videoMessageSizeCalculator.incomingAvatarSize = avatarSize
layout.videoMessageSizeCalculator.outgoingAvatarSize = avatarSize
layout.photoMessageSizeCalculator.incomingAvatarSize = avatarSize
layout.photoMessageSizeCalculator.outgoingAvatarSize = avatarSize
// ... etc for all message types

That's not to say that users couldn't just create an extension to do this, but if the maintenance cost is indeed low, why not just add to the API for a better dev experience?

@ljs19923
Copy link

@ndonald2 do you how can i am align the bottom date to the right ? Without avatars

@SD10
Copy link
Member

SD10 commented May 10, 2018

@JulienLevallois please keep all discussions on topic

Sent with GitHawk

@ljs19923
Copy link

Its on the topic.. its about the avatar view

@nathantannar4
Copy link
Member

@JulienLevallois Take a look at func cellBottomLabelAlignment(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LabelAlignment . This is shown in the example project, please take a good look at it as I think it would help for a lot of your questions.

@Artheyn
Copy link

Artheyn commented May 24, 2018

@JulienLevallois @SD10 , I've faced the same issues exposed by Julien and I've pushed a PR to fix this: #699

@stale
Copy link

stale bot commented Jun 14, 2018

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 14, 2018
@stale
Copy link

stale bot commented Jun 21, 2018

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 21, 2018
@commando24
Copy link

Now your can use
func avatarSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize { return .zero }
to hide avatar.

@Artheyn
Copy link

Artheyn commented Jun 29, 2018

Thanks for your reply @commando24 . In fact, this method has been deprecated since MessageKit 1.0:

    func avatarSize(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGSize {
        fatalError("avatarSize(for:at:in) has been removed in MessageKit 1.0.")
    }

But if you use the latest beta 1.0.0-beta.2 the issue is fixed (by #716).

You can now directly use :

private func setupCollectionView() {
    guard let flowLayout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout else { return }

    flowLayout.attributedTextMessageSizeCalculator.outgoingAvatarSize = .zero
    flowLayout.attributedTextMessageSizeCalculator.incomingAvatarSize = .zero
}

@n13
Copy link

n13 commented Jun 9, 2019

I should probably have checked this thread first. I was implementing the old delegate methods, and noticed they never get called. Duh. Ok. Then eventually found what @Artheyn said above.

guard let flowLayout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout else { return }

flowLayout.attributedTextMessageSizeCalculator.outgoingAvatarSize = .zero
flowLayout.attributedTextMessageSizeCalculator.incomingAvatarSize = .zero

@matsotaa
Copy link

Hey! nice pod)
I've got a problem, when I hide avatars in configureAvatarView:
when I type there
if let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout { layout.textMessageSizeCalculator.outgoingAvatarSize = .zero layout.textMessageSizeCalculator.incomingAvatarSize = .zero }

and if in func messageReceived I typed messagesCollectionView.scrollToBottom(animated: true) then.. in the moment when I entered in any chat, which have more messages than screen able to show scrollToBottom performs, but after it scrolls back to top.
How can I stop this scrolling(to top)

@le-hu-dev
Copy link

messagesCollectionView.messagesCollectionViewFlowLayout.setMessageOutgoingAvatarSize(.zero)
it works.
messageKit 3.4.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests