Jim
Jim

Reputation: 9234

iPhone: relocate subview

I have a custom UIView. in initWithFrame I add image

 - (id)initWithFrame:(CGRect)frame
{
 self = [super initWithFrame:frame];
if (self) {
     self.bgImage = [[[UIImageView alloc] initWithFrame:
        CGRectMake(frame.size.width - 10, 20, 50 , 50)] autorelease];
    [self.bgImage setImage:[UIImage imageNamed:@"myImage.png"];];
}    
return self;
}

Everything is fine when I create my custom UIView. But when I change it's frame, my image position didn't changed. How to fix that ? Tried with autoresize mask, but it didn't help.

Upvotes: 0

Views: 62

Answers (1)

mattjgalloway
mattjgalloway

Reputation: 34912

Override layoutSubviews like so:

- (void)layoutSubviews {
    [super layoutSubviews];
    self.bgImage.frame = CGRectMake(frame.size.width - 10.0f, 20.0f, 50.0f, 50.0f);
}

Basically, this gets called whenever a re-layout is needed. So you should set the frame of any subviews as you need to.

Upvotes: 4

Related Questions