1

I have a prototype cell with a UIImageView.

I would like to position it exactly to the left edge of the screen (no space in between like it is by default).

First picture is what I have (default tableView and cells), second is what I try to do.

enter image description here

No matter what I try (frame, bounds, CGAffineTransform on cell.imageView) there is always a space between the edge and the image, it's like iOS prevent this behavior.

I'm not using AutoLayout and would like to do this programmatically.

Solution

#import "ResultCell.h"

@implementation ResultCell 

-(void) layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(-10,0,10,10);
}

@end

See @matt answer for more details.

kursus
  • 1,396
  • 3
  • 19
  • 35
  • Can you post your relevant code here – Bannings Jun 07 '15 at 13:45
  • Can you please post your requirement by image or something like that & what you are getting currently. – iYoung Jun 07 '15 at 15:11
  • Is the problem that the image view doesn't go to the edge of the cell, or the table view doesn't go to the edge of the screen? How are you positioning the table view itself? – rdelmar Jun 07 '15 at 16:36
  • @rdelmar first proposition, the table is default. See updated answer with picture. – kursus Jun 07 '15 at 17:04
  • @RajatDeepSingh I've added an example picture. – kursus Jun 07 '15 at 17:05
  • possible duplicate of [UITableViewCell imageView padding](http://stackoverflow.com/questions/15924412/uitableviewcell-imageview-padding) – kovpas Jun 07 '15 at 17:07

1 Answers1

2

it's like iOS prevent this behavior

You are absolutely right. You are repositioning the image view, but then the cell is coming along and positioning it back again in its layoutSubviews.

The solution is to use a custom cell class where you override layoutSubviews. For example:

class MyCell : UITableViewCell {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.imageView!.frame.origin.x = 0
    }
}

You will have to make various other arrangements, of course, in order to make sure that a MyCell instance is actually used instead of the default UITableViewCell. But that is left as an exercise for the reader!

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Discussed in more detail in my book: http://www.apeth.com/iOSBook/ch21.html#_overriding_a_cell_8217_s_subview_layout - which also provides other ways of doing this that I think are better, namely, providing your own custom subviews and not using the default `imageView` at all. – matt Jun 07 '15 at 17:13
  • Hi @kursus - let's debug it! First, put a breakpoint in that `layoutSubviews` and let's make sure it's even being called and that `self.imageView` is not nil. Then we can proceed from there... – matt Jun 07 '15 at 18:11
  • By the way, you can see that it does work, simply by downloading and running this sample project: https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/iOS7bookExamples/bk2ch08p402overrideCellLayout/ch21p702overrideCellLayout – matt Jun 07 '15 at 18:12
  • Thank you I will compare to see what I'm doing wrong ! – kursus Jun 07 '15 at 18:17
  • Ok I was calling a standard cell in insertRow, it's working thanks a lot ! – kursus Jun 07 '15 at 22:06
  • Yup, that's why I said "You will have to make various other arrangements, of course, in order to make sure that a MyCell instance is actually used instead of the default UITableViewCell". There are a lot of ways that can go wrong! :) – matt Jun 07 '15 at 22:28