ios popover

字数 179阅读 340

popover (iphone && ipad)


_setVC 为设置界面;

添加 <UIPopoverPresentationControllerDelegate>;

- (void)leftBtnClick:(UIButton*)sender{


    if(_setVC) {

        [self dismissViewControllerAnimated:YES completion:nil];

        _setVC=nil;

    }

    _setVC = [[ASettingViewController alloc] init];

    [_setVC setDelegate:self];

    //设置弹出的样式为popover

    _setVC.modalPresentationStyle = UIModalPresentationPopover;

    //设置弹出控制器的尺寸

    _setVC.preferredContentSize = CGSizeMake(120, 120);

    //设置popoverPresentationController的sourceRect和sourceView属性

    _setVC.popoverPresentationController.sourceRect = sender.bounds;

    _setVC.popoverPresentationController.sourceView = sender;

    //设置箭头方向

    _setVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;

    //设置背景色,包括箭头

    _setVC.popoverPresentationController.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.5];

    _setVC.popoverPresentationController.delegate = self;

    //弹出

    [self presentViewController:_setVC animated:YES completion:nil];

}


#pragma mark - UIPopoverPresentationControllerDelegate

//实现该代理方法,返回UIModalPresentationNone值,可以在iPhone设备实现popover效果

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController*)controller{

    return UIModalPresentationNone;//不适配(不区分ipad或iPhone)

}