drawing a uiview offscreen

2019-08-27 16:05发布

I want create a UIView that's offscreen when ViewDidLoad is called, but that I will animate up onto the screen once a certain function has been called. The code for animating the UIView is fine, but I can't seem to draw the UIView offscreen to begin with (I've assembled the UIView in storyboard onto my UIViewControleller). This is the code I've tried:

- (void)viewDidLoad {

    ...

     [_tagView setFrame:CGRectMake(0, self.view.frame.size.height+40, self.view.frame.size.width, 40)];

    ...

}

_tagView is drawn where I've placed it in the StoryBoard editor, which is at the bottom of my UIViewController.

1条回答
来,给爷笑一个
2楼-- · 2019-08-27 16:26

A couple of things: A view object's frame gives it's coordinates in it's parent view's coordinate space.

A view's bounds are it's internal coordinate system.

When you set a view's frame, it should be expressed in terms of it's parent's bounds, not it's parent's frame.

So your code should be creating your _tagView's frame in terms of self.view.bounds, not self.view.frame.

Next, are you using AutoLayout or struts and springs? In Xcode 5 everything defaults to AutoLayout. You can't change frames and bounds when AutoLayout is active. Instead you have to change the relevant constraints. If you want to set a view's frame manually, you probably want to turn off AutoLayout and use "struts and springs" style view geometry.

EDITED: I moved some of the info from comments up into my original answer for clarity:

If you want to move a view at runtime with Auto Layout active, you have to find the constraint for the setting you want to change, (width, height, leading edge, trailing edge, etc.) and control-drag from the constraint into your .h file to make an outlet. Then in your code you change the constant property associated with the constraint. – Duncan C 22 mins ago

Note that you CAN turn Auto Layout off on a storyboard-by-storyboard or NIB-by-NIB basis. If you select the file and choose the File Inspector, there is a checkbox "Use Autolayout" hidden in the "Interface Builder Document" section. I always have trouble finding it.

Here is a screen-shot showing the location of the button in the Xcode project window (far right side)enter image description here

查看更多
登录 后发表回答