Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone dev - Set position of a view in viewDidLoad

So, in Interface Builder I have a view within the superview that contains two Image View objects. I'd like to move that view off the screen when the app launches so it can be animated to move into place. The view is described as pictureFrame in the .h file for the interface, and I have the view mapped to the outlet pictureFrame. Here is my current viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    CGRect theFrame = [self.pictureFrame frame];
    theFrame.origin.y = -290;
}

But, it doesn't seem to be working. How do I fix this?

like image 589
Hunter Bridges Avatar asked Dec 04 '22 13:12

Hunter Bridges


2 Answers

You forget to set your view's frame:

CGRect theFrame = [self.pictureFrame frame];
    theFrame.origin.y = -290;

add this and you're good:

self.pictureFrame.frame = theFrame;

like image 63
ahmet emrah Avatar answered Dec 16 '22 02:12

ahmet emrah


I tend to do these sorts of things as one liners. I find it easier to remember what is going on when I go back and read it later.

I also prefer to #define my view offsets to keep magic numbers out of my code.

#define kPictureFrameHorizontalOffset -290


- (void)viewDidLoad {
    [super viewDidLoad];
    self.pictureFrame.frame = CGRectMake(self.pictureFrame.frame.origin.x + 0,
                                         self.pictureFrame.frame.origin.y + kPictureFrameHorizontalOffset,
                                         self.pictureFrame.frame.size.width + 0,
                                         self.pictureFrame.frame.size.height + 0);
}

Granted it is a bit more verbose, but it works well for me.

like image 37
Brad The App Guy Avatar answered Dec 16 '22 01:12

Brad The App Guy