#import "ViewController.h"
#import "RedViewController.h"
#import "CustomPresentationController.h"
@interface ViewController () <UIViewControllerTransitioningDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
RedViewController *vc = [[RedViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.modalPresentationStyle = UIModalPresentationCustom;
nav.transitioningDelegate = self;
[self presentViewController:nav animated:YES completion:nil];
}
#pragma mark - UIViewControllerTransitioningDelegate
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented
presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
CustomPresentationController *vc = [[CustomPresentationController alloc] initWithPresentedViewController:presented
presentingViewController:presenting];
return vc;
}
@end
@implementation RedViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.view.layer.cornerRadius = 16;
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doneItemAction:)];
self.navigationItem.leftBarButtonItem = doneItem;
}
- (void)doneItemAction:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@implementation CustomPresentationController
- (CGRect)frameOfPresentedViewInContainerView
{
return CGRectMake(0, 200, self.containerView.width, self.containerView.height);
}
@end
Demo: https://gitee.com/lazyoung/modal-view-demo.git
参考文章自定义拖拽:https://kittenyang.com/uipresentation/
网友评论