UIViewController呈现另一个UIViewController

时间:2011-10-07 09:46:37

标签: objective-c uiview uiviewcontroller addsubview

我有一个UIVutController 1 UIButton(添加为子视图),在我按下Button(见下面的图1)之后添加另一个UIViewController 2,动画从下到上经过一些动作:

[[[UIApplication sharedApplication] keyWindow] addSubview:self.view];

它与UIButton重叠,如何添加不包含UIButton的子视图(参见下面的图2)

pic.1:

enter image description here

PIC。 2:

enter image description here

2 个答案:

答案 0 :(得分:2)

您尚未显示动画代码,但我假设您在屏幕外添加子视图,然后更改其框架(动画)以将其滑入视图。

使用insertSubview:belowSubview:添加新的子视图,将您的按钮作为第二个参数传递。这样,按钮将与新视图重叠,而不是相反。 addSubview:始终将新视图置于其他任何视图之上。

修改

根据您的评论,您似乎正在使用presentModalViewController:将第二个视图控制器添加到屏幕,因此上述方法无效。据我所知,如果您以这种方式呈现它,则无法在新视图控制器视图的顶部保留原始视图控制器中的元素。

您可能需要创建新的UIWindow并将其windowLevel设置为UIWindowLevelAlert才能按住您的按钮。这将使其保持在下面的任何视图之上。将此窗口作为子视图添加到主窗口。

答案 1 :(得分:1)

{
SecondViewController *objComing=[[SecondViewController alloc] init];
[self.view addSubview:objComing.view];

objComing.view.backgroundColor=[UIColor blueColor];

objComing.view.frame=CGRectMake(0,420, 320, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];

[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
objComing.view.frame=CGRectMake(0,0, 320, 420);

[UIView commitAnimations];

}

将此代码放在按钮操作中,并将secondViewController替换为ViewController。