一种方法是将“半模态”控制器添加为子视图控制器,并为其视图设置动画。对于此示例,我在情节提要中创建了“半模态”控制器,其框架高度为 4 英寸 iPhone 屏幕的一半。您可以使用更多动态方法来考虑不同的屏幕尺寸,但这应该可以帮助您入门。
@interface ViewController ()
@property (strong,nonatomic) UIViewController *modal;
@end
@implementation ViewController
- (IBAction)toggleHalfModal:(UIButton *)sender {
if (self.childViewControllers.count == 0) {
self.modal = [self.storyboard instantiateViewControllerWithIdentifier:@"HalfModal"];
[self addChildViewController:self.modal];
self.modal.view.frame = CGRectMake(0, 568, 320, 284);
[self.view addSubview:self.modal.view];
[UIView animateWithDuration:1 animations:^{
self.modal.view.frame = CGRectMake(0, 284, 320, 284);;
} completion:^(BOOL finished) {
[self.modal didMoveToParentViewController:self];
}];
}else{
[UIView animateWithDuration:1 animations:^{
self.modal.view.frame = CGRectMake(0, 568, 320, 284);
} completion:^(BOOL finished) {
[self.modal.view removeFromSuperview];
[self.modal removeFromParentViewController];
self.modal = nil;
}];
}
}