美文网首页
iOS UIPopoverPresentationControl

iOS UIPopoverPresentationControl

作者: CaptainRoy | 来源:发表于2018-11-06 15:34 被阅读3次
屏幕快照 2018-11-06 下午3.33.35.png
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10.0f, 30.0f, 50.0f, 30.0f);
    [button setTitle:@"菜单" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor orangeColor]];
    [button addTarget:self action:@selector(popClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
-(void)popClick:(UIButton *)button
{
    UIViewController *testVC = [[UIViewController alloc] init];
//    testVC.view.backgroundColor = [UIColor yellowColor];
    testVC.preferredContentSize = CGSizeMake(200.0f, 200.0f);
    testVC.modalPresentationStyle = UIModalPresentationPopover;
    
    UIPopoverPresentationController *popver = [testVC popoverPresentationController];
    popver.delegate = self;
    popver.permittedArrowDirections = UIPopoverArrowDirectionUp; // 箭头位置
    popver.sourceView = button; // 设置目标视图
    popver.sourceRect = CGRectMake(30.0f, button.frame.origin.y, 10.0f, 0.0f); // 弹出视图显示位置
    popver.backgroundColor = [UIColor redColor];
    [self presentViewController:testVC animated:YES completion:NULL];
    
}

#pragma mark - UIPopoverPresentationControllerDelegate
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
{
    return YES;
}

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}

相关文章

网友评论

      本文标题:iOS UIPopoverPresentationControl

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