以编程方式将 UIButtons 添加到 UICollectionView 单元格的 uiimage

Posted

技术标签:

【中文标题】以编程方式将 UIButtons 添加到 UICollectionView 单元格的 uiimage【英文标题】:Adding UIButtons to uiimage of a UICollectionView cells programmatically 【发布时间】:2014-11-12 05:02:03 【问题描述】:

我有一个 UIViewController,其中有一个 UICollectionView 以编程方式在其中创建的图库。我想在 UICollectionView 单元格的每个 uiimage 上添加一个按钮:CMFViewController.h.m 文件中用于向 imageview 添加按钮的代码是:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

    CMFGalleryCell *cell = (CMFGalleryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    NSString *imageName = [self.dataArray objectAtIndex:indexPath.row];
    [cell setImageName:imageName];
   // [[cell myButton] addTarget:self action:@selector(myClickEvent:event) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(80.0, 210.0, 100.0, 20.0);
    [button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"ShowView" forState:UIControlStateNormal];

  //  NSMutableArray *buttonArray = [NSMutableArray arrayWithCapacity:100];

  //  for (int i = 0; i < 4; i++) 
       // NSUInteger index = arc4random() % 4;


        // newButton.frame = CGRectMake( 5,  5, 10, 10);    // Whatever position and size you need...

       // UIImage *buttonImage = [UIImage imageNamed:[self.dataArray objectAtIndex:indexPath.row]];
        //[newButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

     //   [buttonArray addObject:newButton];
  //  
   // newButton = buttonArray;   // Where myButtons is a NSArray property of your class, to store references to the buttons.

   // [newButton addTarget:self action:@selector(loadImages:) forControlEvents:UIControlEventTouchUpInside];
    //[newButton addTarget:self action:@selector(updateCell) forControlEvents:UIControlEventTouchUpInside];

    [cell updateCell];

    return cell;


我已通过以下链接从代码中获得帮助 adding-a-uibutton-to-a-uicollectionview

UIButton in cell in collection view not receiving touch up inside event 请给我你的建议并帮助我解决这个问题。

从这个链接你可以在Creating a Paged Photo Gallery With a UICollectionView 中下载source code:他们我想将下一个和上一个按钮放在屏幕每个图像的中间行

【问题讨论】:

向单元格添加按钮。 [cell addSubview: button]; 请在源代码上尝试在每个图像上放置下一个和上一个按钮。我试过 [cell addSubview: button];但它不工作.. 只需在单元格的 xib 中执行此操作即可。拖出两个按钮并将它们放在图像视图上您想要的位置。给他们 centerY 约束,以及你想要的与图像视图两侧的距离。 按钮显示在单元格中。我检查源并添加按钮。但如果您想使用下一个以前的功能,您也可以在CMFViewController.xib 上添加按钮。你也可以试试@rdelmar 评论。 @Wwwwwwwwwwwww 我编辑了代码检查它 【参考方案1】:

为了实现下一个和上一个按钮,不要在每个图像上添加它,最好将它添加到集合视图中,chinttu-maddy-ramani 在他的回答中所说的是正确的,但他在 xib 文件中执行,但以编程方式你可以像下面那样做,

只需添加此代码

编辑要求更新按钮

CMFViewController.h 文件中创建两个按钮属性 替换为以下代码

 #import <UIKit/UIKit.h>

 @interface CMFViewController : UIViewController <UICollectionViewDataSource,   UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
 @property (nonatomic, strong) UIButton *nextButton;
 @property (nonatomic, strong) UIButton *prevButton;
 @end

CMFViewController.m 文件中

 - (void)viewDidLoad
  
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self loadImages];
    [self setupCollectionView]; 
    [self addNextAndPreviousButtons];//add a method to add previous and next buttons 
 

 //hear u are adding previous and next images, but i didn't added the constraints u add it if u want t work in both orientation 
 - (void)addNextAndPreviousButtons
 
    _nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _nextButton.frame =CGRectMake(self.view.bounds.size.width - 40, self.view.bounds.size.height/2, 50, 30);
    [_nextButton setTitle:@"Next" forState:UIControlStateNormal];
    [_nextButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    _prevButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _prevButton.frame =  CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.height/2, 50, 30);
    [_prevButton setTitle:@"Prev" forState:UIControlStateNormal];
    [_prevButton addTarget:self action:@selector(previousButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_nextButton];
    [self.view addSubview:_prevButton];



 //previous button action
- (void)previousButtonAction:(id)sender

    NSArray *visibleCell = [_collectionView visibleCells];
    if(visibleCell)
    
      NSIndexPath *path = [_collectionView indexPathForCell:[visibleCell lastObject]];
      if(!path.row == 0)
      
         NSIndexPath *nextPath = [NSIndexPath indexPathForItem:(path.row - 1) inSection:0];
         [_collectionView scrollToItemAtIndexPath:nextPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
     
   


//next button action
- (void)nextButtonAction:(id)sender  

    NSArray *visibleCell = [_collectionView visibleCells];
    if(visibleCell)
   
      NSIndexPath *path = [_collectionView indexPathForCell:[visibleCell lastObject]];
     if(path.row < [_dataArray count] - 1)
     
         NSIndexPath *nextPath = [NSIndexPath indexPathForItem:(path.row + 1) inSection:0];
         [_collectionView scrollToItemAtIndexPath:nextPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
  


//edited the method addNextAndPreviousButtons please replace it by this edited answer
//for all cases even when u are swiping 
//this is to handle buttons either by tapping or navigation through swiping the collection view 
- (void)updateButtons:(NSInteger)row
 
    //at the beginning
    if(row == 0)
    
      _prevButton.hidden = YES;
      _nextButton.hidden = NO;
      return;
    
    else if(row == ([_dataArray count] -1))
    
      _nextButton.hidden = YES;
      _prevButton.hidden = NO;
    
    else
    
      _nextButton.hidden = NO;
      _prevButton.hidden = NO;
    
 

//finally u have to call updateButtons method  this method 
 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
 
   CMFGalleryCell *cell = (CMFGalleryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

   NSString *imageName = [self.dataArray objectAtIndex:indexPath.row];
   [cell setImageName:imageName];
   [cell updateCell];
   [self updateButtons:indexPath.row]; //update the buttons 
   return cell;

 

【讨论】:

请更改此代码以隐藏前一张图片上的上一个按钮和最后一张图片上的下一个按钮 把 else 条件放在 if 条件下面并隐藏相应的按钮或禁用用户交互 请编码...我如何隐藏第一张图片的上一个按钮和最后一张图片的下一个按钮 - 是的.. :) 等待我正在测试所有条件等待 @Wwwwwwwwwwwww 检查它我编辑如果你有任何问题只是评论【参考方案2】:

您可以在单元格中添加按钮以直接在CMFGalleryCell.xib 中拖放按钮,请查看附件截图。并添加按钮约束以使您的单元格水平居中和垂直居中。

仅供参考如果您想要下一个上一个功能,那么您还可以在 CMFViewController.xib 的主 xib 中添加按钮,如下图所示。

【讨论】:

@Wwwwwwwwwwwww 你知道如何在xib中拖放按钮吗?

以上是关于以编程方式将 UIButtons 添加到 UICollectionView 单元格的 uiimage的主要内容,如果未能解决你的问题,请参考以下文章

即使在图像被更改或删除后,以编程方式添加的 UIButtons 也会保留背景图像

使用选择器操作将多个UIButtons添加到UIView

将 UIButtons 添加到 ViewDidLoad 上的数组

自动居中以编程方式创建的 UIButtons

更改以编程方式创建的单击 UIButton 的背景图像

如何以编程方式为一组 UIButtons 设置单独的背景图像