简体   繁体   English

iPhone dev - 在viewDidLoad中设置视图的位置

[英]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. 因此,在Interface Builder中,我在superview中有一个包含两个Image View对象的视图。 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. 该视图在接口的.h文件中被描述为pictureFrame,我将视图映射到outlet pictureFrame。 Here is my current viewDidLoad: 这是我当前的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? 我该如何解决?

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; self.pictureFrame.frame = theFrame;

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我的视图偏移来保持我的代码中的魔术数字。

#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. 虽然它有点冗长,但对我来说效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM