At what point can I safely access a frame in a UIImageView subclass?

I have subclassed UIImageView. After

self = [super initWithImage:image]

      

I am trying to access the self.frame.origin.y value. But it's always 0.0. A moment later, in an externally called method, the property is completely there with a good 100.0.

I also tried overriding - (id) initWithFrame: (CGRect) aRect, but the result is the same. Is this property properly set once the view is added to some control? I am convinced that I am trying to get the rectangle immediately after the object has been selected and initialized, but before it is added to the supervisor. This will happen one line later.

0


a source to share


2 answers


I can't reproduce this problem, but I need to interpolate a bit to guess where you expect 100 to be from. Where do you install it? This seems to work fine:

- (id)initWithImage:(UIImage*)image {
    self = [super initWithImage:image];
    if (self != nil)
    {
        CGRect frame = self.frame;
        frame.origin = CGPointMake(100.0, 100.0);
        self.frame = frame;
        NSLog(@"y=%d", self.frame.origin.y);
        NSLog(@"frame=%@", NSStringFromCGRect(self.frame));
    }
    return self;
}

      



Perhaps you could provide a little more code?

+1


a source


An object frame can be set at any time. If you want to keep track of changes in the frame, override setFrame:

andsetBounds:



- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    // Your code here
}
- (void)setBounds:(CGRect)bounds
{
    [super setBounds:bounds];
    // Your code here
}

      

+3


a source







All Articles