我正在尝试以编程方式将{image>添加到collectionView
的顶部(类似于tableViewHeaderView
),但到目前为止,我在viewDidLoad
尝试的不是工作得很好
UIImageView *headerImageView = [[UIImageView alloc] init];
if ([isHeaderVisible intValue]== YES) {
NSLog(@"Header View was found.");
[headerImageView setImage:[UIImage imageNamed:headerImage]];
[headerImageView setUserInteractionEnabled:YES];
[headerImageView setFrame:CGRectMake(0, 0, 320, 160)];
[headerImageView setContentMode:UIViewContentModeScaleAspectFit];
}
else {
NSLog(@"No Header view found.");
[headerImageView setImage:nil];
[headerImageView setFrame:CGRectMake(0, 0, 0, 0)];
}
是否找到标题视图的逻辑正在起作用,但我无法使UIImageView
起作用。任何帮助将不胜感激!
P.S。这不适用于章节标题视图,它类似于Apple App Store中的headerView
。
更新
我也在viewController
中使用了一个标题标题。所以基本上我想使用节标题和视图标题。如何使用以下内容创建节标题和视图标题:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
DetailCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
headerView.sectionTitle.text = collectionSectionTitle;
headerView.backgroundImage.image = [UIImage imageNamed:@"WFSectionHeader.png"];
reusableview = headerView;
}
if (kind == UICollectionElementKindSectionFooter) {
UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
reusableview = footerview;
}
return reusableview;
}
答案 0 :(得分:0)
使用uicollectionview
的委托方法之一-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *view;
if(kind == UICollectionElementKindSectionHeader)
{
view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
//_scroll.frame = view.frame;
[view addSubview:_scroll];
_scroll.center = CGPointMake(self.view.frame.size.width / 2, _scroll.center.y);
//_scroll.contentSize = CGSizeMake(view.frame.size.width * (MAXBANNER - 1),_scroll.frame.size.height);
_scroll.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
}
return view;
}
答案 1 :(得分:0)
工作代码。尝试将subView添加到reusableView。直接设置imageView之前将headerView分配给可重用使用它不接受。它会给出错误: - - [UICollectionReusableView setImage:]:无法识别的选择器发送到实例0xa17be70
因为在reusableView.So中没有像setImage这样的属性。准备一个带有图像的视图,并将subView添加到可重用的View中。 将headerView分配给reusableView后,将imageView作为子视图添加到可重用视图中。
- (void)viewDidLoad
{
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
UIView *headerView =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
headerView.backgroundColor=[UIColor greenColor];
UIImageView *imageView=[[UIImageView alloc] initWithFrame:headerView.frame];
[imageView setImage:[UIImage imageNamed:@"btn-bg.png"]];
reusableview = (UICollectionReusableView*)headerView;
[reusableview addSubview:imageView];
}
if (kind == UICollectionElementKindSectionFooter) {
UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
reusableview = footerview;
}
return reusableview;
}