如何以编程方式创建视图并根据超级视图设置其框架? (iPhone)
我完全迷失了 superview、loadView()、viewDidLoad()、viewWillAppear()。
最终,我想将控制器的框架设置为超级视图的边界,并在 viewDidLoad() 中使用 [self.viewbounds]。 (也许这是不可能的?)
这是让我悲伤的代码片段。
// add the controller's view to the scroll view
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];
// I'd like to set controller.view 's frame using superview(scrollView here).
}
显然,nil ==controller.view.superview调用了控制器的viewDidLoad。
我曾经访问 [self.view borders] 并在我的 viewDidLoad 中使用它
因为我想在将视图添加到超级视图后设置视图的框架,所以我想我需要将利用 [self.viewbounds] 的代码移动到其他地方。
搬到什么地方好呢? 或者有更好的方法吗?
I'm totally lost with superview, loadView(), viewDidLoad(), viewWillAppear().
Eventually, I want to set my controller's frame to superview's bounds and utilize [self.view bounds] at my viewDidLoad(). (maybe this is impossible?)
here's the code snippet that gives me grief..
// add the controller's view to the scroll view
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];
// I'd like to set controller.view 's frame using superview(scrollView here).
}
Apparently, nil == controller.view.superview calls the controller's viewDidLoad.
I used to access [self.view bounds] and use it inside my viewDidLoad
Since I want to set view's frame after adding it to superview, I guess I need to move the codes that utilizes [self.view bounds] to somewhere else.
What is the good place to move to?
Or Is there better approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一次调用
controller.view
和self.view
会自动触发视图加载机制和每个视图控制器的 viewDidLoad 通知(我假设self
code> 是一个不同的视图控制器)。因此,每个控制器的 viewDidLoad 方法将在此代码片段的开头调用。(
loadView
是一个让你手动重写视图加载过程的方法。你不应该直接调用它)The first call to
controller.view
andself.view
automatically triggers the view loading mechanism and the viewDidLoad notification for each view controller (I'm assumingself
is a different view controller). Thus the viewDidLoad methods for each controller will be called at the beginning of this code snippet.(
loadView
is a method for you to manually override the view loading process. You shouldn't call it directly)我将总结问题和我的解决方案。
问题本质上是viewDidLoad和addSubview这两个函数的调用顺序不能调换。
我想要的是,
根据superview设置child的frame,只能在addSubview之后完成。
但同时,在 viewDidLoad 上,我有正确的 [self.view 边界]。
由于 viewDidLoad 必须在 addSubview 之前调用,因此 viewDidLoad 将无法获得正确的 [self.viewbounds] 信息。
解决方案,
我声明了一个 superViewBounds 变量,并在分配视图控制器之后但创建视图之前设置它们。并在viewDidLoad中使用了bounds变量。
还剩下一件事,我仍然不知道如何获取 navigationViewController 的主视图(我认为它称为导航视图)边界/框架信息。
希望它有所帮助,并希望有人发布更好的解决方案(如果有)。
I'll summarize the problem and my solution.
The problem is , in essence, that the order of two function calls: viewDidLoad and addSubview can not be switched.
What I want is,
set the frame of child according to superview which could only be done after addSubview.
But at the same time, upon viewDidLoad, I have the proper [self.view bounds].
Since viewDidLoad would have to be called before addSubview, viewDidLoad wouldn't be able to have the correct [self.view bounds] info.
Solution,
I declared a superViewBounds variable and set them after allocing the view controller but before creating the view. And used the bounds variable inside viewDidLoad.
One thing remains, I still don't know how I could get the main view(I think it's called navigation view) bounds/frame info of navigationViewController.
Hope it helps, and hope someone post a better solution if there is.