Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the image to top-left corner of uicollectionView cell in ios?

I want to add button to the top-left corner of UICollectionViewCell. I am able to add the button on top left of cell but it does not appear on top of the cell, but under the cell. Please tell me how can I do that ?

I am using below code:
enter image description here

_deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, delButtonSize, delButtonSize)];
    _deleteButton.center = CGPointMake(9, 10);
    _deleteButton.backgroundColor = [UIColor clearColor];

    [_deleteButton setImage: [UIImage imageNamed:@"cross_30.png"] forState:UIControlStateNormal];
    [cell addSubview:_deleteButton];

    [_deleteButton addTarget:self action:@selector(deleteRecipe:) forControlEvents:UIControlEventTouchUpInside];

Edit:
I want to make delete icon like this:
enter image description here

like image 283
TechChain Avatar asked Nov 07 '15 08:11

TechChain


Video Answer


2 Answers

Did you try adding it to the content view of the cell? [cell.contentView addSubview:_deleteButton];

like image 64
user754905 Avatar answered Sep 28 '22 20:09

user754905


use bringSubviewToFront in UIView:

[cell bringSubviewToFront:_deleteButton];

reference: UIView bringSubviewToFront

to achieve the effect on the top left corner out of cell's bound, you should follow Islam Q's answer by move the deletebutton's frame outside of cell's bound, and set cell's clip subview to NO.

like image 25
Allen Avatar answered Sep 28 '22 20:09

Allen