popoverPresentationController UIPopoverController 使用方法詳解(氣泡控制器)


 之前iPad特有的控件,現在iPhone亦可使用。

 點擊按鈕,彈出popOverVC.

按鈕的點擊事件:

- (IBAction)pickOrderAction:(UIButton *)sender {
    
    // > 初始化要彈出的控制器
    UIViewController * vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor blueColor];
     // > 設置彈出的控制器的顯示樣式
    vc.modalPresentationStyle = UIModalPresentationPopover;
     // > 彈出模式
    vc.modalTransitionStyle   = UIModalTransitionStyleFlipHorizontal;
    // > 彈出控制器的尺寸
    vc.preferredContentSize = CGSizeMake(300, 150);
    // > 彈出控制器的箭頭指向的view
    vc.popoverPresentationController.sourceView = sender;
    // > 彈出視圖的箭頭的“尖”的坐標 - 以sourceView的(0,0,0,0)為基准結合sourceRect。系統默認width/2使用。(sender.bounds的位置即:在sender的底部邊緣居中)
    vc.popoverPresentationController.sourceRect = sender.bounds;
    // > 箭頭的指向(上,下,左,右)
    vc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    // > presentVC
    [self presentViewController:vc animated:YES completion:nil];
}

 ENOUGH TO USE?

iPad使用以上方法就可實現彈出功能

 

以下是iPhone的實現方法

TipViewController 是一個自定義的UIViewController

使用時一定要設置代理。並實現此代理方法;

效果如圖:

 

 

 /// 設置氣泡 - 提示開播時間
        TipViewController * tipVC  = [[TipViewController alloc] init];
        tipVC.preferredContentSize = CGSizeMake(192, 56);
        tipVC.modalPresentationStyle = UIModalPresentationPopover;
        UIPopoverPresentationController * popVC = tipVC.popoverPresentationController;
        popVC.delegate = self;
        popVC.sourceView = startBtn;
        popVC.sourceRect = CGRectMake(startBtn.frame.size.width/2.0, 0, 0, 0);
        [self presentViewController:tipVC animated:YES completion:nil];
需要遵守的協議 UIPopoverPresentationControllerDelegate
#pragma mark - popViewController 的代理方法,實現該方法才能夠局部彈出控制器
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{

    return UIModalPresentationNone;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM