Search code examples
iosobjective-ccocoa-touchiboutletcollection

frame of different objects in IBOutletCollection returns empty


I created 8 different objects (4 pairs of UIImageView and textview) on the storyboard and connect each of them to a IBOutletCollection, but when I do this

for (UIView *element in infoElements) {
    NSLog(@"frame: %@", NSStringFromCGRect(element.frame));
}

it returns

frame: {{0, 0}, {0, 0}}

[infoElements count] returns correctly 8

and

NSLog(@"element: %@", element);

returns correctly the object, only the frame is missing. I only need the position (x and y) of every object in the IBOutletCollection.

The initialization is like this:

@interface infoViewController : UIViewController{
    IBOutletCollection(UIView) NSArray *infoElements;
}

@property (nonatomic, retain) IBOutletCollection(UIView) NSArray *infoElements;

I googled it for many hours now and didn't find that anyone had the same problem (except this unanswered question here), so I think that I'm just doing something wrong

Edit

Just to clarify, the objects in the IBOutletCollection aren't in the position 0,0 and are bigger than 0,0


Solution

  • I guess that you are using Auto Layout. In this case the frame won't be set until layoutSubviews has been called.

    So if you need to get the frame you can do that in - (void)viewDidLayoutSubviews. However, if you are planning on manipulating the frame you should do that with Constraints when using Auto Layout.

    Another option would be to just turn off Auto Layout in your Storyboard.