Flea
Flea

Reputation: 11284

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

Upvotes: 9

Views: 8255

Answers (4)

Shalini Kamala
Shalini Kamala

Reputation: 1

The easiest way would be to define a custom cell, set Accessory to none, place an imageview that acts on behalf of the accessory view wherever you like, and define a tap gesture handler for the cell (if you want to limit tap gesture to only the image, just add tap gesture only to imageview instead of entire cell). This gives a lot of flexibility, plus we don't have to deal with the annoyances that accessoryview brings (for me at least - i dislike the border on the right of the cell)

In your custom cell setup code:

let tap = UITapGestureRecognizer(target: self, action: #selector(onCellTapped(_:)))
self.contentView.addGestureRecognizer(tap)

@objc private func onCellTapped(_: Any) {
   //your code here - i added an imageview that changes image on user tap - to indicate selected/deselected
}

Upvotes: 0

Flea
Flea

Reputation: 11284

Based on Matt's tip the only way I found to do this was to just use my own UIImageView and use the image of my choice and then set the frame coordinates to what I needed. This allowed the accessoryView to expand. Apparently this is the only way to manipulate it.

UIImage *indicatorImage = [UIImage imageNamed:@"BV-icon_57x57"];
        UIImageView *view = [[UIImageView alloc] initWithImage:indicatorImage];
        view.frame = CGRectMake(0, 0, 100, 20);
        [view setContentMode:UIViewContentModeLeft];//without this line the image will just be stretched;
        cell.accessoryView = view;

Upvotes: 0

Yunus Nedim Mehel
Yunus Nedim Mehel

Reputation: 12369

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; });

Upvotes: 1

Wain
Wain

Reputation: 119031

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.

Upvotes: 7

Related Questions