以编程方式创建一个视图,并将框架设置为超级视图的框架?
我有一个基于导航的应用程序,我将自定义视图放置在导航视图中。
首先,我想知道如何获取“导航视图”的框架,以便我可以在其上正确放置自定义视图。
另外,我有时会隐藏导航栏并希望确保正确获取框架信息。 (不适用于 [[UIScreen mainScreen] 边界] )
其次,一般来说,如何访问超级视图的框架以便我可以相应地放置视图?
我尝试了以下方法但没有成功。
-(void) loadView {
[super loadView];
UIView* myView = [[UIView alloc] initWithFrame: CGRectZero];
self.view = myView;
self.view.frame = self.view.superview.frame;
[myView release];
}
或在layoutSubviews中设置框架,但注意到layoutSubviews没有被调用。我想那是因为我在这个特定视图中没有任何子视图?
谢谢
-编辑-
给出的答案,我现在知道我需要将 view.frame 设置为超级视图的边界。
问题是我无法访问 viewDidLoad 内的超级视图。
我可以看到 superview 在 viewWillAppear 中正确设置,但在 viewDidLoad 中设置不正确。
我发现 viewDidLoad 在不同的时间被调用,例如当被 PushViewController 推送或访问 self.view
我有以下代码,viewDidLoad 是从 nil ==controller.view.superview 调用的。
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
由于我无法从 viewDidLoad 访问超级视图,因此我无法像以前在 viewDidLoad 中那样布局视图。这看起来很不方便而且不必要的复杂。
请问有什么建议吗?
I have a navigation based application where I place my custom views in the navigation view.
First, I wanna know how to get the "navigation view"'s frame so that I can place my custom view on it properly.
Also, I sometimes hide navigation bar and want to make sure I get the frame info correctly. (not with [[UIScreen mainScreen] bounds] )
Second, in general, how do I access the superview's frame so that I can place a view accordingly?
I tried the following with no success.
-(void) loadView {
[super loadView];
UIView* myView = [[UIView alloc] initWithFrame: CGRectZero];
self.view = myView;
self.view.frame = self.view.superview.frame;
[myView release];
}
or setting the frame in layoutSubviews, but noticed layoutSubviews not getting called. I guess that's because I don't have any subviews in this particular view?
Thank you
-Edit-
with given answers, i now know that i need to set the view.frame to superview's bounds.
The problem is that i'm unable to access superview inside viewDidLoad.
I can see that superview is correctly set in viewWillAppear but not in viewDidLoad.
I found out that viewDidLoad gets called at various times, such as when pushed by pushViewController or accessing self.view
I have the following code and viewDidLoad gets called from nil == controller.view.superview.
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
Since I can't access superview from viewDidLoad, i can't layout the view as i previously did in viewDidLoad. This seems so inconvenient and unnecessarily complex.
Any suggestion please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的视图作为子视图添加到某处之前,
superview
属性为零。一般来说,您无法知道loadView
方法中的超级视图是什么。您可以按照 Costique 的建议使用自动调整大小属性。查看官方文档中的调整子视图大小 UIView 文档。
您还可以在调用
loadView
后设置框架,例如如下所示:顺便说一句,使用框架的父级的
bounds
属性通常更安全子视图的。这是因为父视图的边界与子视图的框架位于同一坐标系中,但父视图的框架可能不同。例如:The
superview
property is nil until your view is added as a subview somewhere. In general, you can't know what the superview will be within theloadView
method.You could use autoresizing properties, as suggested by Costique. Check out the Resizing Subviews in the official UIView docs.
You could also set the frame after the call to
loadView
, for example in something like this:By the way, it is generally safer to use the parent's
bounds
property for the frame of the subview. This is because a parent's bounds are in the same coordinate system as the subview's frame, but the parent's frame may be different. For example:只需设置自动调整大小蒙版,导航控制器就会完成剩下的工作:
Just set the autoresizing mask and the navigation controller will do the rest: