关于UIPopoverPresentationController的使用

标签: iOS 开发

效果就是点击按钮弹出一个带箭头的弹框效果图如下在这里插入图片描述
具体实现过程如下
先创建一个继承UIViewController视图控制器ZButtonListView 内容根据你的需要去写主要结构如下
ZButtonListView.h文件

@interface ZButtonListView : UIViewController

-(void)creatUIView;

@end

ZButtonListView.m文件


@interface ZButtonListView()

@property (nonatomic) UIScrollView* scrollView;

@property (assign, nonatomic) CGSize popSize;

@property (nonatomic) NSMutableArray*dataArray;

@end

@implementation ZButtonListView

- (id)initWithSize:(CGSize)size

{

    _popSize= size;

    self= [super init];

    if(self) {

        _dataArray=[[NSMutableArrayalloc]init];

        // Initialization code

    }

    return self;

}

-(void)dealloc
{
    for(UIView*vv in self.view.subviews)
    {
        [vvremoveFromSuperview];
    }
    _scrollView = nil;
    _dataArray = nil;
}

-(void)creatUIView{

    [self getDataArray];//获取数据

    CGRectframe =CGRectMake(0,0,_popSize.width,_popSize.height);

    _scrollView= [[UIScrollViewalloc]initWithFrame:frame];

    _scrollView.showsVerticalScrollIndicator = NO;//不显示垂直方向的滚动条

    self.view = _scrollView;

    [self setData];//放数据

}

点击切换按钮时调用的方法

-(void)selectSizeType:(UIButton*)btn{
        // 显示弹窗
        [self.listView creatUIView];
        UIPopoverPresentationController *popover = [self.listView popoverPresentationController];

        popover.permittedArrowDirections = UIPopoverArrowDirectionUp;//设置箭头位置

        popover.sourceView=btn;//设置目标视图

        popover.sourceRect= btn.bounds;//弹出视图显示位置

        popover.delegate=self;

       //如果self是一个UIViewController可以直接用    

       // [self presentViewController:self.listView animated:NO completion:nil];

       //我的self不是一个UIViewController

       ZMainViewController* mainControl = [ZMainViewController mainViewController];

       [mainControl presentViewController:self.listView animated:NO completion:nil];

}

-(ZButtonListView *)listView{

    if(_listView==nil) {

        _listView=[[ZButtonListViewalloc]initWithSize:CGSizeMake(200,300)];

        _listView.preferredContentSize = CGSizeMake(200, 300);// 设置浮窗的宽高

        _listView.modalPresentationStyle = UIModalPresentationPopover;

        _listView.listDelegate=self;

    }

    return _listView;

}
原文链接:加载失败,请重新获取