Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override accessoryView in UITableViewCell to change it's position

By default the accessoryView on the UITableViewCell is positioned to the far right on the cell. I am sub-classing the UITableViewCell to try and change this position to move it over more the left, however, it has no effect and it remains to the right. Any ideas on how to do this?

- (void) layoutSubviews
{
    [super layoutSubviews];
    self.accessoryView.frame = CGRectMake(100, self.accessoryView.frame.origin.y, self.accessoryView.frame.size.width, self.accessoryView.frame.size.height);
    self.accessoryView.layer.borderWidth = 1;
    self.accessoryView.layer.borderColor = [[UIColor redColor] CGColor];
}

enter image description here

like image 630
Flea Avatar asked May 27 '13 20:05

Flea


2 Answers

Implement layoutSubviews in your cell subclass, call super as the first thing you do and then modify the frames of the subviews you want to change.

like image 88
Wain Avatar answered Oct 25 '22 22:10

Wain


This is similar to Flea's solution; pushes the image by the given amount instead of a fixed frame:

static CGFloat const kRightOffset = 10.0;

cell.accessoryView = ({UIImageView * imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image.png"]];
                CGRect frame = imgV.frame;
                frame.size.width = frame.size.width + kRightOffset;
                imgV.frame = frame;
                [imgV setContentMode:UIViewContentModeLeft];
                imgV; });
like image 1
Yunus Nedim Mehel Avatar answered Oct 25 '22 21:10

Yunus Nedim Mehel