Reputation: 11284
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];
}
Upvotes: 9
Views: 8255
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
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
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
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