11

In iOS 6 and earlier a uitableviewcell's imageView was positioned all the way over to the left with a 0 offset. In iOS 7 though this has been changed and there is now a 15 point space now. I would like to position the imageView like it is in iOS 6. I'm already subclassing the uitableviewcell with AKHighlightableAttributedCell to deal with attributed text not being highlighted. So based on some searching I added:

- (void) layoutSubviews
{
    [super layoutSubviews];
    // Makes imageView get placed in the corner
    self.imageView.frame = CGRectMake( 0, 0, 80, 80 );
}

The issue is everything else still doesn't get repositioned and so I'm thinking there must be a better way to do this. I'd read some people mentioning using a negative offset to move everything over but I wasn't sure how this would work with constraints as it needs to scale properly for each orientation. Is there an easier solution to this that I'm missing? Thank you.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Brian F Leighty
  • 922
  • 11
  • 22
  • 1
    If you want custom layout then this is the proper approach. You just need to set the frame of the labels too. – rmaddy Sep 29 '13 at 15:57
  • Well perhaps a better question would be this. I added // Get textlabel frame CGRect textlabelFrame = self.textLabel.frame; textlabelFrame.origin.x = 90; self.textLabel.frame = textlabelFrame; Which takes care of the title position. But then I was thinking, won't this also shift over the contents on the left as well if the title is long enough? Also, there is border that separates the cells and I don't know what the name of that view or property is. Thanks for the help. – Brian F Leighty Sep 30 '13 at 03:22

1 Answers1

7

It appears I was doing it the correct way. The missing piece regarding the divider between fields was setting the inset on iOS 7. You can do this in the viewdidload or viewwillload and set self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);

You will need to add a check if running iOS 7 or newer as this is a new property I believe. A better option might be setting it in the storyboard by selecting the table view and then setting separator insets from default to custom.

Here is the layoutSubviews method that repositions imageView and textLabel. If you have a description add that as well.

- (void) layoutSubviews
{
    [super layoutSubviews];
    // Makes imageView get placed in the corner
    self.imageView.frame = CGRectMake( 0, 0, 80, 80 );

    // Get textlabel frame
    //self.textLabel.backgroundColor = [UIColor blackColor];
    CGRect textlabelFrame = self.textLabel.frame;

    // Figure out new width
    textlabelFrame.size.width = textlabelFrame.size.width + textlabelFrame.origin.x - 90;
    // Change origin to what we want
    textlabelFrame.origin.x = 90;

    // Assign the the new frame to textLabel
    self.textLabel.frame = textlabelFrame;
}
Brian F Leighty
  • 922
  • 11
  • 22
  • 1
    is the only way to stop iOS7 adding the padding either side of a cell.imageView is to subclass the cell and apply the above because the default behaviour doesn't respond to setting the imageView frame and insets to zero? – Alex McPherson Oct 02 '13 at 08:33
  • 1
    If there is, I couldn't find it. It's a bit of a pain and would of been nice for them to expose an option for it. I feel that for borderless images for the tableviewcells, it's a huge pain but at least it's possible to get around it. I haven't seen any issues with the way I've implemented it so far. I'm open to a better answer if it exists though. – Brian F Leighty Oct 03 '13 at 03:33