我认为最好使用自定义容器控制器方法来执行此操作。在下面的示例代码中,我制作了一个表单大小 (540 x 600) 的登录控制器,并将其从右侧滑入 ViewController 的主视图,使其垂直居中,并靠在右边。
在 ViewController.m 中,我有这个:
-(IBAction)showLogin:(id)sender {
LoginController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"Login"];
login.view.frame = CGRectMake(768, 202, 540, 600);//centered vertically and offscreen to the right
[self addChildViewController:login];
[self.view addSubview:login.view];
[UIView animateWithDuration:.5 animations:^{
login.view.frame = CGRectMake(228, 202, 540, 600);
} completion:^(BOOL finished) {
[login didMoveToParentViewController:self];
}];
}
为了删除视图,我在 LoginController.m 中有这个:
-(IBAction)goBackToMain:(id)sender {
[UIView animateWithDuration:.5 animations:^{
self.view.frame = CGRectMake(768, 202, 540, 600);
} completion:^(BOOL finished) {
[self.view removeFromSuperview];
[self removeFromParentViewController];
}];
}
编辑后:
如果要从 ViewController 中的按钮中删除登录控制器,可以这样做:
-(IBAction)goBackToMain:(id)sender {
LoginController *login = self.childViewControllers.lastObject;
[UIView animateWithDuration:.5 animations:^{
login.view.frame = CGRectMake(768, 202, 540, 600);
} completion:^(BOOL finished) {
[login.view removeFromSuperview];
[login removeFromParentViewController];
}];
}