【问题标题】:Add the button at the bottom of UICollectionview in iOS在 iOS 的 UICollectionview 底部添加按钮
【发布时间】:2014-08-22 04:50:14
【问题描述】:

我有一个问题:我想要 UICollectionview 底部的 1 个按钮以允许用户选择所有照片。该按钮是“全选”按钮,当滚动该按钮时,该按钮始终位于集合视图的底部。当我单击该按钮时,将检查收藏视图中的所有照片。我尝试添加页脚,但这不是我想要的。我不知道如何添加该按钮。请给我一些建议。提前致谢。

编辑:包含图片:

你可以在这里看到图片:1

集合视图底部的“全选”按钮。该按钮将在用户长按 5 秒后出现。如果用户单击此按钮,所有照片将被选中。我该怎么做?

【问题讨论】:

  • 能否请您详细解释一下,或者可以提供示例代码或应用程序屏幕截图?
  • 您可以尝试添加另一个部分,其中仅包含一个带有按钮的项目。
  • @user3884889 上传图片兄弟,清楚地解释您的查询。
  • 将 self.view 上的按钮添加为子视图,并使用 Bringsubviewtofrontview 在所有视图上方显示您的按钮。
  • 嗨,我也面临同样的问题。我在 UICollectionView 下方添加了一个按钮。但是当 iPhone 处于纵向模式时它不可见。但按钮在横向模式下可见。

标签: ios objective-c uibutton uicollectionview


【解决方案1】:

在我看来,您应该在集合视图下方添加一个 UIButton 并使集合视图的框架更短。这样,您将能够让按钮操作操纵集合视图,并且始终在底部可见

UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0,200,320,50)];
[self.contentView addSubview:button];

【讨论】:

  • 我想在uicollectionview前面添加这个按钮。你可以请检查我的编辑?谢谢
  • 是的,只需在集合视图前面添加该按钮,它不必是该集合视图的一部分。将两者都单独添加到视图中
  • 我不知道如何将该按钮放在 collectionview 前面。你能给出一些示例代码吗?谢谢。
  • 您是在使用界面构建器还是以编程方式完成这一切?只需做类似的事情
  • 我正在以编程方式进行所有操作。我为 uicollectionview 编写了一个自定义类,以便将它用于许多视图控制器。
【解决方案2】:
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0,200,320,50)];

[self.view addSubview:button];

如果你想把视图放在前面,你可以选择一些 API。

bringSubviewToFront:

移动指定的子视图,使其出现在其兄弟视图之上。

sendSubviewToBack:

移动指定的子视图,使其出现在其兄弟视图的后面。

insertSubview:atIndex:

在指定索引处插入子视图。

insertSubview:aboveSubview:

在视图层次结构中的另一个视图上方插入一个视图。

更多详情,flow Apple doc

【讨论】:

    【解决方案3】:

    如果您希望全选按钮始终位于底部,那么您应该将其添加到视图中,并确保在添加集合视图后添加按钮,并且它将始终位于底部。

    您可以像这样在集合视图上添加长按手势:

    - (void)viewDidLoad
    {
        // attach long press gesture to collectionView
        UILongPressGestureRecognizer *lpgr
        = [[UILongPressGestureRecognizer alloc]
           initWithTarget:self action:@selector(handleLongPress:)];
        lpgr.minimumPressDuration = .5; //seconds
        lpgr.delaysTouchesBegan = YES;
        lpgr.delegate = self;
        [self.collectionView addGestureRecognizer:lpgr];
    }
    
    -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
    {
        if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
            return;
        }
        CGPoint p = [gestureRecognizer locationInView:self.collectionView];
    
        NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
        if (indexPath == nil){
            NSLog(@"couldn't find index path");
        } else {
            UIButton *btnSelectAll = [UIButton buttonWithType:UIButtonTypeCustom];
            btnSelectAll.frame = CGRectMake(110,437,100,30);
            [[btnSelectAll titleLabel] setFont:[UIFont systemFontOfSize:18]];
            [btnSelectAll setImage:[UIImage imageNamed:@"selectImage"] forState:UIControlStateNormal];
            [btnSelectAll addTarget:self action:@selector(selectAllEvent:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:btnSelectAll];
        }
    }
    
    -(IBAction)selectAllEvent:(id)sender{
    
        //Write code to select all images
    
    }
    

    完成后不要忘记将按钮从其超级视图中删除。我认为这会有所帮助。

    【讨论】:

    • 您的代码运行良好。但是如何在 UICollectionview 的底部添加该按钮。我尝试了您的代码,但滚动时无法在底部屏幕添加。请帮我。谢谢
    • 尝试设置背景颜色或按钮可见的东西。
    • 是的,按钮已显示,但其位置不正确。我怎样才能将它始终添加到集合视图的底部
    • 相应地设置按钮框架 CGRectMake(110,437,100,30);有什么大不了的?
    • 问题是当我滚动collectionview时按钮没有显示在底部。
    猜你喜欢
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    相关资源
    最近更新 更多