【问题标题】:Adding View to top of AQGridView ScrollView将视图添加到 AQGridView ScrollView 的顶部
【发布时间】:2012-06-14 16:21:22
【问题描述】:

我正在使用 AQGridView 创建一个包含大约 21 个项目的图片库。我想在此列表/滚动视图的顶部添加一个包含应用程序徽标和名称的视图,以便用户看到它,但当他们滚动查看图像时,带有名称和徽标的视图会随之滚动。以前有没有人实施过这样的事情?

感谢@skram,我能够添加徽标,但现在图像被第一行图像挡住了。

这是我定义徽标并将其添加到 gridView 的地方:

 self.gridView = [[AQGridView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
        UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"paramythLogoTest.png"]];
        [logo setFrame:CGRectMake(0,0,logo.frame.size.width,logo.frame.size.height)]; 
        [gridView addSubview:logo];
        self.gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        self.gridView.autoresizesSubviews = YES;
        self.gridView.delegate = self;
        self.gridView.dataSource = self;



        [self.view addSubview:gridView];


        [self.gridView reloadData];

然后图片在这里加载:

- (AQGridViewCell *) gridView: (AQGridView *) aGridView cellForItemAtIndex: (NSUInteger) index
{
static NSString * PlainCellIdentifier = @"PlainCellIdentifier";

GridViewCell * cell = (GridViewCell *)[aGridView dequeueReusableCellWithIdentifier:@"PlainCellIdentifier"];

if ( cell == nil )
{
    cell = [[GridViewCell alloc] initWithFrame: CGRectMake(3.333, 3.333, 100, 100)
                               reuseIdentifier: PlainCellIdentifier];
}
NSString *stringURL = [[featuredStories objectAtIndex:index] objectAtIndex:1];

NSURL *url = [NSURL URLWithString:stringURL];
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"example0.png"]];
cell.storyID = [[featuredStories objectAtIndex:index] objectAtIndex:0];\

return cell;
}

这是模拟器的截图:

【问题讨论】:

    标签: iphone ios aqgridview


    【解决方案1】:

    是的。您可以使用addSubview: 轻松完成此操作。在您的 AQGridView 实例上,添加一个 UIImageView 作为子视图。 AQGridView 只不过是 UIScrollView 的子类,您可以在其上使用 addSubview:

    ....
    AQGridView *_grid;
    ....
    UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
    [logo setFrame:CGRectMake(0,0,logo.frame.size.width,logo.frame.size.height)]; // Set the Frame's position on where you want it in the grid.
    [_grid addSubview:logo];
    

    希望这会有所帮助。

    编辑:编辑答案以反映将徽标作为第一个子视图添加到AQGridView,以便所有图像与徽标重叠。而不是使用[_grid addSubview:logo],假设这些是UIImageView的使用:

    [_grid insertSubview:logo belowSubview:(UIImageView*)[_grid.subviews objectAtIndex:0]];
    

    由 mkral 编辑

    虽然 skram 版本确实有效,但我想提一下,出于我的目的,gridViewHeader 是最佳选择,请参阅代码:

    UIImageView *logo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"paramythLogoTest.png"]];
    [logo setFrame:CGRectMake(0,0,logo.frame.size.width,logo.frame.size.height)]; 
    [gridView setGridHeaderView:logo]; 
    

    【讨论】:

    • 好的,它可以工作,但它会覆盖图像。你知道我需要在哪里设置偏移量吗?
    • 您可以使用insertSubview:belowSubview。这将做的是将您的徽标添加为图层中的第一个对象。之后的所有内容都将添加到上面的图层上:[_grid insertSubview:logo belowSubview:(UIImageView*)[_grid.subviews objectAtIndex:0]]; Assuming it's a UIImageView`
    • 我在您的示例中有点困惑,您不是说要在同一个徽标图像视图下方插入徽标图像视图吗?
    • 如果您的滚动视图中已经有图像,则没有。它正在获取滚动视图中已经存在的第一个项目,大概是其中一个图像。如果不是这种情况,则每次添加新图像时,您可能必须执行sendSubviewToBack
    • 哦,好吧,我先添加徽标子视图。是否有 AQGridView 的委托方法,例如 afterCellsLoaded 之类的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    相关资源
    最近更新 更多