设置一些气泡的最小宽度
Setting minimum width for some bubbles
我想知道为某些气泡设置最小宽度的正确方法是什么。我已经尝试覆盖 applyLayoutAttributes: 在我的自定义单元格中,但我需要访问我的数据源才能知道哪个单元格应该具有最小宽度。我一直在修改 cellForItemAtIndexPath: 中的 messageBubbleLeftRightMargin: 但没有结果。
任何指针都会很棒
编辑
我的 Message 模型(符合 )有一个标志告诉我气泡是否需要有最小宽度
在我的自定义单元格中,我可以覆盖自定义布局,但我无权访问数据源,因此我无权访问该标志
-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
JSQMessagesCollectionViewLayoutAttributes *customAttributes = (JSQMessagesCollectionViewLayoutAttributes *)layoutAttributes;
// I need access to my <JSQMessageData> for the condition
if (condition) {
if(customAttributes.messageBubbleContainerViewWidth <175){
customAttributes.messageBubbleContainerViewWidth = 175;
}
}
[super applyLayoutAttributes:customAttributes];
}
我也尝试在我的 JSQMessagesViewController 子类中访问左右约束,但无济于事
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
/**
* Override point for customizing cells
*/
JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];
BLMessage *message = [self.visibleMessagesArray objectAtIndex:indexPath.item];
JSQMessagesCollectionViewLayoutAttributes *customAttributes = [JSQMessagesCollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
if(message.isEphemeral){
//random number to see if it had an effect
self.collectionView.collectionViewLayout.messageBubbleLeftRightMargin = 200;
//I also tried modifying the customAttributes
//customAttributes.messageBubbleContainerViewWidth = 175;
}
return cell;
}
我是 UICollectionViewFlowLayout 等方面的新手,我可能会遗漏一些核心概念
好的,所以我发现出了什么问题。我没有将新布局应用于单元格,所以我的 cellForItemAtIndexPath 最终变成了这样:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
/**
* Override point for customizing cells
*/
JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];
BLMessage *message = [self.visibleMessagesArray objectAtIndex:indexPath.item];
JSQMessagesCollectionViewLayoutAttributes *customAttributes = (JSQMessagesCollectionViewLayoutAttributes *)[self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
if(message.isEphemeral){
//I also tried modifying the customAttributes
customAttributes.messageBubbleContainerViewWidth = 175;
[cell applyLayoutAttributes: customAttributes];
}
return cell;
}
------------编辑------------
我之前的回答有一些副作用,不建议在你的collectionView:cellForItemAtIndexPath:
中调用applyLayoutAttributes:
。
正确的做法是继承 JSQMessagesCollectionViewFlowLayout
CustomCollectionViewFlowLayout.h
#import "JSQMessagesCollectionViewFlowLayout.h"
@interface CustomCollectionViewFlowLayout : JSQMessagesCollectionViewFlowLayout
@property (strong, nonatomic) UIImageView *incomingBubbleMask;
@end
CustomCollectionViewFlowLayout.m
#import "CustomCollectionViewFlowLayout.h"
#import "JSQMessage.h"
#import "JSQMessagesCollectionView.h"
#import "JSQMessagesCollectionViewCell.h"
#import "JSQMessagesCollectionViewLayoutAttributes.h"
#import "JSQMessagesCollectionViewFlowLayoutInvalidationContext.h"
#import "UIImage+JSQMessages.h"
@implementation CustomCollectionViewFlowLayout
+ (Class)layoutAttributesClass
{
return [JSQMessagesCollectionViewLayoutAttributes class];
}
- (CGSize)messageBubbleSizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize superSize = [super messageBubbleSizeForItemAtIndexPath:indexPath];
JSQMessage *currentMessage = (JSQMessage *)[self.collectionView.dataSource collectionView:self.collectionView messageDataForItemAtIndexPath:indexPath];
/*********** Setting size **************/
//check if outgoing, you can import your Session Manager to check your user identifier
if ([currentMessage.senderId isEqualToString:@"me") {
superSize = CGSizeMake(175, superSize.height);
}
//do whatever other checks and setup your width/height accordingly
return superSize;
}
@end
不要忘记在 JSQMessagesViewController
子类中设置自定义布局,#import "CustomCollectionViewFlowLayout.h"
并在 viewDidLoad
中添加:self.collectionView.collectionViewLayout = [[CustomCollectionViewFlowLayout alloc] init];
我想知道为某些气泡设置最小宽度的正确方法是什么。我已经尝试覆盖 applyLayoutAttributes: 在我的自定义单元格中,但我需要访问我的数据源才能知道哪个单元格应该具有最小宽度。我一直在修改 cellForItemAtIndexPath: 中的 messageBubbleLeftRightMargin: 但没有结果。 任何指针都会很棒
编辑
我的 Message 模型(符合 )有一个标志告诉我气泡是否需要有最小宽度
在我的自定义单元格中,我可以覆盖自定义布局,但我无权访问数据源,因此我无权访问该标志
-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
JSQMessagesCollectionViewLayoutAttributes *customAttributes = (JSQMessagesCollectionViewLayoutAttributes *)layoutAttributes;
// I need access to my <JSQMessageData> for the condition
if (condition) {
if(customAttributes.messageBubbleContainerViewWidth <175){
customAttributes.messageBubbleContainerViewWidth = 175;
}
}
[super applyLayoutAttributes:customAttributes];
}
我也尝试在我的 JSQMessagesViewController 子类中访问左右约束,但无济于事
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
/**
* Override point for customizing cells
*/
JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];
BLMessage *message = [self.visibleMessagesArray objectAtIndex:indexPath.item];
JSQMessagesCollectionViewLayoutAttributes *customAttributes = [JSQMessagesCollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
if(message.isEphemeral){
//random number to see if it had an effect
self.collectionView.collectionViewLayout.messageBubbleLeftRightMargin = 200;
//I also tried modifying the customAttributes
//customAttributes.messageBubbleContainerViewWidth = 175;
}
return cell;
}
我是 UICollectionViewFlowLayout 等方面的新手,我可能会遗漏一些核心概念
好的,所以我发现出了什么问题。我没有将新布局应用于单元格,所以我的 cellForItemAtIndexPath 最终变成了这样:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
/**
* Override point for customizing cells
*/
JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];
BLMessage *message = [self.visibleMessagesArray objectAtIndex:indexPath.item];
JSQMessagesCollectionViewLayoutAttributes *customAttributes = (JSQMessagesCollectionViewLayoutAttributes *)[self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
if(message.isEphemeral){
//I also tried modifying the customAttributes
customAttributes.messageBubbleContainerViewWidth = 175;
[cell applyLayoutAttributes: customAttributes];
}
return cell;
}
------------编辑------------
我之前的回答有一些副作用,不建议在你的collectionView:cellForItemAtIndexPath:
中调用applyLayoutAttributes:
。
正确的做法是继承 JSQMessagesCollectionViewFlowLayout
CustomCollectionViewFlowLayout.h
#import "JSQMessagesCollectionViewFlowLayout.h"
@interface CustomCollectionViewFlowLayout : JSQMessagesCollectionViewFlowLayout
@property (strong, nonatomic) UIImageView *incomingBubbleMask;
@end
CustomCollectionViewFlowLayout.m
#import "CustomCollectionViewFlowLayout.h"
#import "JSQMessage.h"
#import "JSQMessagesCollectionView.h"
#import "JSQMessagesCollectionViewCell.h"
#import "JSQMessagesCollectionViewLayoutAttributes.h"
#import "JSQMessagesCollectionViewFlowLayoutInvalidationContext.h"
#import "UIImage+JSQMessages.h"
@implementation CustomCollectionViewFlowLayout
+ (Class)layoutAttributesClass
{
return [JSQMessagesCollectionViewLayoutAttributes class];
}
- (CGSize)messageBubbleSizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize superSize = [super messageBubbleSizeForItemAtIndexPath:indexPath];
JSQMessage *currentMessage = (JSQMessage *)[self.collectionView.dataSource collectionView:self.collectionView messageDataForItemAtIndexPath:indexPath];
/*********** Setting size **************/
//check if outgoing, you can import your Session Manager to check your user identifier
if ([currentMessage.senderId isEqualToString:@"me") {
superSize = CGSizeMake(175, superSize.height);
}
//do whatever other checks and setup your width/height accordingly
return superSize;
}
@end
不要忘记在 JSQMessagesViewController
子类中设置自定义布局,#import "CustomCollectionViewFlowLayout.h"
并在 viewDidLoad
中添加:self.collectionView.collectionViewLayout = [[CustomCollectionViewFlowLayout alloc] init];