Vashishtha Jogi
Vashishtha Jogi

Reputation: 12210

UIView subViews not displaying

I am sure this is a very stupid issue. I am trying to add UIImageView as subview to UIView. The code that I am trying for adding subview is:

for (int i = 0; i < kBoardSize; i++) {
    for (int j = 0; j < kBoardSize; j++) {

        Tile *t = [[Tile alloc] initWithPosition:CGPointMake(i, j) withSize:CGSizeMake(width, height)];

        [self insertSubview:t atIndex:0];
        //[self addSubview:t];
        //[self bringSubviewToFront:t];

        [temp addObject:t];
    }
}

The corresponding Tile(which is a UIImageView subclass) code is:

- (id) initWithPosition:(CGPoint)position withSize:(CGSize)size
{
    self = [super initWithFrame:CGRectZero];
    [self setFrame:CGRectMake(position.x * size.width, position.y * size.width, size.width, size.height)];
    self.tag = position.x * kBoardSize + position.y;
    [self setPiece:EMPTY];
    if (self) {
        [self.layer setBorderColor:[[UIColor blackColor] CGColor]];
        [self.layer setBorderWidth:0.5];
    }
    return self;
}

Now when I do

NSLog(@"%@", [[self subViews] description]);

2012-05-22 23:20:51.740 XXXXXXX[8204:f803] (
    "<Tile: 0x6a79380; baseClass = UIImageView; frame = (0 0; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x6a7ad70>>",
    "<Tile: 0x6a94180; baseClass = UIImageView; frame = (0 106.667; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 1; layer = <CALayer: 0x6a93e40>>",
    "<Tile: 0x6a97da0; baseClass = UIImageView; frame = (0 213.333; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 2; layer = <CALayer: 0x6a97de0>>",
    "<Tile: 0x6a7aeb0; baseClass = UIImageView; frame = (106.667 0; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 3; layer = <CALayer: 0x6a7aef0>>",
    "<Tile: 0x6a94610; baseClass = UIImageView; frame = (106.667 106.667; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 4; layer = <CALayer: 0x6a94650>>",
    "<Tile: 0x6a946c0; baseClass = UIImageView; frame = (106.667 213.333; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 5; layer = <CALayer: 0x6a94700>>",
    "<Tile: 0x6a94770; baseClass = UIImageView; frame = (213.333 0; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 6; layer = <CALayer: 0x6a947b0>>",
    "<Tile: 0x6a94820; baseClass = UIImageView; frame = (213.333 106.667; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 7; layer = <CALayer: 0x6a94860>>",
    "<Tile: 0x6a948d0; baseClass = UIImageView; frame = (213.333 213.333; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 8; layer = <CALayer: 0x6a94910>>"
)

It shows that the subviews are added properly. But the subviews dont actually show up in the view. What might be the issue?

Upvotes: 0

Views: 1444

Answers (1)

Oleg Trakhman
Oleg Trakhman

Reputation: 2082

Vashishtha Jogi, you should take a look on The Designated initializer, and how does it work. Main point is that any of initSomething method calls designated initializer eventually, so it is important to call it properly.

Upvotes: 1

Related Questions