popover带箭头弹框
生活随笔
收集整理的這篇文章主要介紹了
popover带箭头弹框
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我們先來看一下效果吧:
?
分析:這個帶箭頭的彈框其實是一個控制器,通過Modal方式展現(xiàn),但跟傳統(tǒng)模態(tài)方式效果不一樣,我們一眼就能看出。
?
Xib方式實現(xiàn)popover:
1、segue的時候選擇Present As Popover
2、我們看下segue的屬性:
3、重寫prepareforsegue方法:
1 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 2 // 1、判斷跳轉控制器的類型 3 if segue.destination.isKind(of: YSSportMapModeVC.self){ 4 5 // 2、驗證popover和傳統(tǒng)模態(tài)之間的區(qū)別 6 // 只有為popover時,popoverPresentationController有值,否則為nil 7 // print(segue.destination.popoverPresentationController!) 8 9 let vc:YSSportMapModeVC = segue.destination as! YSSportMapModeVC 10 11 // 3、設置代理 12 vc.popoverPresentationController?.delegate = self 13 14 // 4、其實我們展現(xiàn)的還是segue.destination,popoverPresentationController只是用來設置展現(xiàn)效果的 15 // 設置展現(xiàn)的控制器的大小,如果width=0,寬度交給系統(tǒng)設置 16 vc.preferredContentSize = CGSize(width: 0, height: 120) 17 18 // 5、設置地圖視圖的顯示模式 19 vc.didSelectedMapMode = {(mapType:MAMapType) in 20 self.mapView.mapType = mapType 21 } 22 23 // 6、設置vc的當前顯示模式 24 vc.currentMapType = mapView.mapType 25 } 26 }4、實現(xiàn)代理方法:
1 // MARK: - UIPopoverPresentationControllerDelegate 2 extension YSSportMapVC:UIPopoverPresentationControllerDelegate{ 3 func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { 4 // 不讓系統(tǒng)自適應 5 return .none 6 } 7 }代碼方式實現(xiàn)popover:
1 #import "ViewController.h" 2 3 @interface ViewController () <UIPopoverPresentationControllerDelegate> 4 5 // 通過xib設置的按鈕,傳統(tǒng)模態(tài)展現(xiàn) 6 @property (weak, nonatomic) IBOutlet UIButton *demoButton; 7 8 @end 9 10 @implementation ViewController 11 12 /** 13 1. 添加加號按鈕 14 2. 點擊加號按鈕以popover的方式`展現(xiàn)`一個視圖控制器 15 */ 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 19 UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd]; 20 btn.center = self.view.center; 21 22 [self.view addSubview:btn]; 23 24 [btn addTarget:self action:@selector(popover:) forControlEvents:UIControlEventTouchUpInside]; 25 } 26 27 - (void)popover:(UIButton *)sender { 28 29 UIViewController *vc = [UIViewController new]; 30 31 vc.view.backgroundColor = [UIColor redColor]; 32 33 // 1. 在 iPhone 上默認是模態(tài)展現(xiàn),設置展現(xiàn)類型為 popover 34 vc.modalPresentationStyle = UIModalPresentationPopover; 35 36 // 設置彈窗視圖控制器視圖的大小 37 vc.preferredContentSize = CGSizeMake(0, 120); 38 39 // 2. 設置展現(xiàn)的代理 40 vc.popoverPresentationController.delegate = self; 41 42 // 3. 指定彈窗的定位控件 - 使用代碼開發(fā),必須設置定位控件 43 vc.popoverPresentationController.sourceView = sender; 44 45 // 4. 設置箭頭方向朝上 46 vc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp; 47 48 // 5. 設置箭頭的位置,原點可以參照某一個控件的尺寸設置,寬高通常用于設置附加的偏移量,通常傳入0即可 49 CGSize size = sender.bounds.size; 50 vc.popoverPresentationController.sourceRect = CGRectMake(size.width * 0.5, size.height, 0, 0); 51 52 // 6. 設置繞開控件,注意,同一時間,只能允許模態(tài)展現(xiàn)一個控制器 53 vc.popoverPresentationController.passthroughViews = @[_demoButton]; 54 55 // 展現(xiàn)控制器 56 [self presentViewController:vc animated:YES completion:nil]; 57 } 58 59 - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller { 60 // 不使用系統(tǒng)默認的展現(xiàn)樣式! 61 return UIModalPresentationNone; 62 } 63 64 @end?
轉載于:https://www.cnblogs.com/panda1024/p/6239125.html
總結
以上是生活随笔為你收集整理的popover带箭头弹框的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: swif-自动引用计数
- 下一篇: 深入浅出UML类图(一)