在屏幕外绘制uiview [英] drawing a uiview offscreen
问题描述
我想创建一个在调用ViewDidLoad时不在屏幕上的UIView,但是一旦调用了某个函数,我将在屏幕上创建动画.动画UIView的代码很好,但我似乎无法从屏幕外画出UIView(我已将故事板上的UIView组装到了UIViewControleller上).这是我尝试过的代码:
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是在将其放置在UIViewController底部的StoryBoard编辑器中绘制的.
_tagView is drawn where I've placed it in the StoryBoard editor, which is at the bottom of my UIViewController.
推荐答案
几件事:视图对象的框架给出了其在父视图坐标空间中的坐标.
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.
因此,您的代码应该根据self.view.bounds而不是self.view.frame来创建_tagView的框架.
So your code should be creating your _tagView's frame in terms of self.view.bounds, not self.view.frame.
接下来,您要使用AutoLayout还是支杆和弹簧?在Xcode 5中,所有内容均默认为AutoLayout.启用自动版式"后,您将无法更改框架和边界.相反,您必须更改相关约束.如果要手动设置视图的框架,则可能要关闭自动布局"并使用支柱和弹簧"样式的视图几何.
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:
如果要在运行时启用自动版式的情况下移动视图,则必须找到要更改的设置的约束(宽度,高度,前缘,后缘等),并从中拖动控件将约束放入您的.h文件中以形成出口.然后,在您的代码中更改与约束关联的常量属性.– 22分钟前邓肯C
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
请注意,您可以逐个故事板或逐个NIB关闭自动布局".如果选择文件并选择文件检查器,则接口生成器文档"部分中将隐藏一个复选框使用自动布局".我总是很难找到它.
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.
这是显示Xcode项目窗口(最右侧)中按钮位置的屏幕截图.
Here is a screen-shot showing the location of the button in the Xcode project window (far right side)
这篇关于在屏幕外绘制uiview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!