How to add button to top left corner in UICollectionViewCell in iOS?

时间:2015-11-24 07:26:21

标签: ios objective-c iphone ipad uicollectionviewcell

I want to add the Button to the top left corner of the cell.I have tried but i always added inside the collection view cell but not on the cell so please tell me how can i o this?

I have tried this.

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BlogAlbumCell  *cell;
    static NSString *identifier = @"UserBlogAlbum";
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    UserAlbum *user_allbum=[arr_userAlbums objectAtIndex:indexPath.row];
    cell.label_blog_name.text=user_allbum.album_name;
    [cell.image_blog_image setImageWithURL:[NSURL URLWithString:[IMAGE_BASE_URL stringByAppendingString:user_allbum.album_image]] placeholderImage:[UIImage imageNamed:@"post_placeholder.png"]];
    if([[[NSUserDefaults standardUserDefaults]valueForKey:@"longPressed"] isEqualToString:@"yes"])
    {
        NSLog(@"Inside the long press section");
        CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        [anim setToValue:[NSNumber numberWithFloat:0.0f]];
        [anim setFromValue:[NSNumber numberWithDouble:M_PI/50]];
        [anim setDuration:0.1];
        [anim setRepeatCount:NSUIntegerMax];
        [anim setAutoreverses:YES];
        cell.layer.shouldRasterize = YES;
        [cell.layer addAnimation:anim forKey:@"SpringboardShake"];
        CGFloat delButtonSize = 30;
        UIButton *delButton = [[UIButton alloc] initWithFrame:CGRectMake(-5,-5, delButtonSize, delButtonSize)];
        delButton.backgroundColor = [UIColor clearColor];
        [delButton setImage: [UIImage imageNamed:@"cross_30.png"] forState:UIControlStateNormal];
       [delButton setBackgroundColor:[UIColor blueColor]];
        [cell addSubview:delButton];
        delButton.tag=indexPath.row;
        [delButton addTarget:self action:@selector(deleteAlbum:) forControlEvents:UIControlEventTouchUpInside];
    }
    else if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"singleTap"] isEqualToString:@"yes"])
    {
        if(indexPath.row==([arr_userAlbums count]-1))
        {
            [[NSUserDefaults standardUserDefaults]setValue:@"no" forKey:@"singleTap"];
        }
        for(UIView *subview in [cell subviews])
        {
            if([subview isKindOfClass:[UIButton class]])
            {
                [subview removeFromSuperview];
            }

        }
        [cell.layer removeAllAnimations];
        // _deleteButton.hidden = YES; 
         [_deleteButton removeFromSuperview];
    }
        return cell;
}

But it does not add the button to the top left.

I want like this

enter image description here

发送数据

修改

这就是我现在所获得的 enter image description here

3 个答案:

答案 0 :(得分:1)

您在Main.storyBoard中的设计如下:

enter image description here

现在,在自定义单元格中创建Button的IBOutlet。 并在 cellForItemAtIndexPath 方法中设置此按钮的属性,如:

// Note that $timeout is now injected to the directive
    angular.module('myApp', []).directive('formInput', function ($timeout) {
        return {
            restrict: 'A',
            //require: '^form',
            link: function (scope, element, attributes, form) {
                $timeout(function() {
                        var input, name;
                        input = element[0].querySelector('input, textarea, select');
                        name = input.getAttribute('name');
                        alert(name);
                }, 0);
                // ...
                // do stuff with input, name, form etc.
            }
        };
    });

并创建 btnDeleteFolderTapped 方法:

[cell.btnDelete setTag:indexPath.row];
[cell.btnDelete addTarget:self action:@selector(btnDeleteFolderTapped:) forControlEvents:UIControlEventTouchUpInside];

表示单元格之间的空格,将下面的代码放在.m文件中:

    -(void)btnDeleteFolderTapped:(id)sender
    {
         // handle your stuff here
    }

然后转到Main.storyboard,单击collectionView设置属性,如:

enter image description here

答案 1 :(得分:0)

因为您将按钮添加为单元格的子视图,当然它将在单元格中添加按钮而不是。例如,您的图标大小为60x60,单元格大小为80x80。只需在该单元格上添加不可见(我的意思是backgroundColor清晰)UIView,其大小与单元格80x80相同,并添加图标作为该视图的子视图,并为此框架CGRectMake(10, 10, 60, 60)添加。最后,在左上角添加图标作为该视图的子视图,如CGRectMake(0, 0, delButtonSize, delButtonSize)。您的UI层次结构应该是这样的;

  

UICollectionViewCell - > UIView - >删除按钮&应用程序图标

答案 2 :(得分:0)

enter image description here我发现了一个简单的黑客行为。

1.拍摄你必须在细胞中显示的图像小于细胞大小并将其对齐到右下角。

  1. 现在只需将按钮添加到单元格剩余空间的左上角。
  2. 如果你是初学者并且不知道如何玩限制,那么我让我解释一下我实际做了什么。

    1. 我让我的手机100X100分和imageView 70X70分。
    2. 然后将给定的imageView右下约束对齐到其超视图,其值为常数" 0"或者ZERO。
    3. 给左和上限约束常数30分。
    4. 添加带有交叉图像图标的按钮。
    5. 使用imageView左对齐按钮,给出-10左对齐约束点
    6. 类似于顶部对齐按钮,其中imageView -10顶部对齐约束点。
    7. 它将提供所需的相同用户体验!我也附上了一张照片。