iOS简单模态视图

#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/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容