iProgrammer
iProgrammer

Reputation: 3107

UIButtons in UITableViewCell for ipad

I created UIButtons in UITableViewCell. It is displaying properly in iphone. But when I upgrade this app for iPad UIbuttons are shifted to right side and come out of the table's boundary . Here is my code

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setFrame:CGRectMake(47.0f, 100.0f, 16, 16.0f)];

[button2 setImage:[UIImage imageNamed:@"Delete.png"] forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button2];



return cell;

Upvotes: 3

Views: 435

Answers (2)

Rohit Wankhede
Rohit Wankhede

Reputation: 506

Please try this one. In you code remove [cell addSubview:button2]; add write there [cell.contentView addSubview:button2]; and also for removing overlapping of cell or for removing disturb buttons frame add this for loop before all cell content view allocated.

for(UIView *view in cell.contentView.subviews)

{

[view removeFromSuperview];

}

then you can add here uibuttons uilabels , etc...

Upvotes: 0

André Morujão
André Morujão

Reputation: 7133

You should probably add the button to the cell's contentView, and not to the cell directly.

You might also want to place the button's frame origin relative to the contentView's frame size, + set the button's autoresizing mask so that it is placed properly according to the cell size (which can change based on device type and/or interface orientation).

Upvotes: 2

Related Questions