开发者

How do you programmatically create a view and set it's frame according to superview? (iphone)

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?


The first call to controller.view and self.view automatically triggers the view loading mechanism and the viewDidLoad notification for each view controller (I'm assuming self 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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜