I want my accessory to be in a slightly different place than normal. Is it possible? This code has no effect:
cell.accessoryType = UITableViewCellAccessoryD
Another way to do this is to embed your custom accessory view in another view, that is set as the cell's accessory view and control the padding using the frame.
Here is an example with an image view as custom accessory view:
// Use insets to define the padding on each side within the wrapper view
UIEdgeInsets insets = UIEdgeInsetsMake(24, 0, 0, 0);
// Create custom accessory view, in this case an image view
UIImage *customImage = [UIImage imageNamed:@"customImage.png"];
UIImageView *accessoryView = [[UIImageView alloc] initWithImage:customImage];
// Create wrapper view with size that takes the insets into account
UIView *accessoryWrapperView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, customImage.size.width+insets.left+insets.right, customImage.size.height+insets.top+insets.bottom)];
// Add custom accessory view into wrapper view
[accessoryWrapperView addSubview:accessoryView];
// Use inset's left and top values to position the custom accessory view inside the wrapper view
accessoryView.frame = CGRectMake(insets.left, insets.top, customImage.size.width, customImage.size.height);
// Set accessory view of cell (in this case this code is called from within the cell)
self.accessoryView = accessoryWrapperView;
Here is what I used, this will get rid of the default padding.
override func layoutSubviews() {
super.layoutSubviews()
// Remove the accessory view's default padding.
accessoryView!.frame.origin.x = bounds.width - accessoryView!.bounds.width - safeAreaInsets.right
contentView.frame.size.width = bounds.width - safeAreaInsets.left - safeAreaInsets.right - accessoryView!.bounds.width
}
No, you cannot move where the accessory view is. As an alternative you can add a subview like the following;
[cell.contentView addSubview:aView];
Also, by setting the accessoryView
property equal to something, the accessoryType
value is ignored.
There is a way to move default accessoryView, but it's pretty hacky. So it might stop working one day when a new SDK arrives.
Use at your own risk (this code snippet moves any accessoryView
8 pixels to the left. Call [self positionAccessoryView];
from inside the -(void)layoutSubviews
method of the desired UITableViewCell
subclass):
- (void)layoutSubviews {
[super layoutSubviews];
[self positionAccessoryView];
}
- (void)positionAccessoryView {
UIView *accessory = nil;
if (self.accessoryView) {
accessory = self.accessoryView;
} else if (self.accessoryType != UITableViewCellAccessoryNone) {
for (UIView *subview in self.subviews) {
if (subview != self.textLabel &&
subview != self.detailTextLabel &&
subview != self.backgroundView &&
subview != self.contentView &&
subview != self.selectedBackgroundView &&
subview != self.imageView &&
[subview isKindOfClass:[UIButton class]]) {
accessory = subview;
break;
}
}
}
CGRect r = accessory.frame;
r.origin.x -= 8;
accessory.frame = r;
}
The simple way to set a custom position for the accessoryView that is persisted in any cell status is to layout the accessoryView in layoutSubViews:
- (void)layoutSubviews
{
[super layoutSubviews];
self.accessoryView.center = CGPointMake($yourX, $yourY);
}
I was able to change the accessory view's frame by simply doing this in my custom cell subclass.
CGRect adjustedFrame = self.accessoryView.frame;
adjustedFrame.origin.x += 10.0f;
self.accessoryView.frame = adjustedFrame;