美文网首页
iOS简单模态视图

iOS简单模态视图

作者: 开发者老岳 | 来源:发表于2023-08-19 12:15 被阅读0次
    #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/

    相关文章

      网友评论

          本文标题:iOS简单模态视图

          本文链接:https://www.haomeiwen.com/subject/twrjmdtx.html