美文网首页
iOS UIPopoverPresentationControl

iOS UIPopoverPresentationControl

作者: Yuency | 来源:发表于2018-10-25 10:13 被阅读26次

    前言:

    以前听到过有一个叫做 POPViewController 的东西, 以前接触过,但是没成功弄出来。
    今天在 UI 稿里再次看到了这个玩意。
    出来混,早晚要还的

    第一步 Command + C

    #import "POPViewController.h"
    
    /// iOS POP 弹出视图 (自行创建一个 ViewController,然后遵守协议 )
    @interface POPViewController () <UIPopoverPresentationControllerDelegate>
    
    @end
    
    @implementation POPViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    //这是从 XIB 中拖出来的按钮点击事件
    - (IBAction)action:(UIButton *)sender {
        
        //新创建一个要显示的控制器
        UIViewController *controller = [[UIInputViewController alloc] init];
        controller.view.backgroundColor = [UIColor greenColor];
        controller.preferredContentSize = CGSizeMake(300, 300);        //popover视图的大小
        controller.modalPresentationStyle = UIModalPresentationPopover;//这句一定要
    
        // 创建一个显示文字的 Label 添加进去
        UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; //Label 的尺寸和位置参考 上面的 controller.preferredContentSize
        l.backgroundColor = [UIColor yellowColor];
        l.numberOfLines = 0;
        l.text = @"My name is Van, I am an artist, a performance artist, I am hired for people to fulfill their fantasies, their deep dark fantasies. I was going to be a movie star, you know, was modeling and acting. After a hundred or two auditions and small parts, you know, I decided.. that I had enough, then I ceded into escort work.";
        [controller.view addSubview:l];
        
        UIPopoverPresentationController *popController = controller.popoverPresentationController;
        popController.delegate = self;  //代理一定要
        popController.sourceView = controller.view; //设置要弹出哪个 视图 (上面自定的控制器的视图)
        popController.sourceRect = CGRectMake(sender.frame.origin.x, sender.frame.origin.y, 0, 0);  //设置弹出框箭头的位置
        [popController setPermittedArrowDirections:UIPopoverArrowDirectionUp]; //设置弹出框箭头的方向
        
        [self presentViewController:controller animated:YES completion:nil];
    }
    
    #pragma mark - 代理
    -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
        return UIModalPresentationNone;
    }
    
    @end
    

    第二步 Command + R

    pop.gif

    相关文章

      网友评论

          本文标题:iOS UIPopoverPresentationControl

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