屏幕快照 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;
}
网友评论