0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOS9 以降の対応 popoverPresentationController

Last updated at Posted at 2020-03-31

UIPopoverController という機能が iOS9.0 以降に
UIPopoverPresentationController に変更してくださいという感じになりました。

どう変えるのか?

って、その前にポップオーバーという仕組みがありまして、どうやら ViewController の上に* ポップオーバー(ううぅ iOSでは一般的なんだけども実は、知られてない用語、下のほうに書いてあるので読んでみてください。)

変更される前の書き方

HogeOldPopOver.m
@property (retain, nonatomic) UIPopoverController *popoverViewController;
@property (retain, nonatomic) PopupMainViewController *popupMainViewController;

/// これまでの書き方 (PopupMainViewController:ポップアップで表示されるViewController)
- (void)oldPopoverAction:(UIButton *)button {

// ポップオーバーしたいViewController の準備
// PopupMainViewController の生成
    if(!self.popupMainViewController){
        self.popupMainViewController = [[PopupMainViewController alloc] initWithNibName:@"PopupMainViewController" bundle:nil];
        self.popupMainViewController.delegate = self;
    }

// UIPopoverController の生成
    if (self.popoverViewController == nil) {
        self.popoverViewController = [[UIPopoverController alloc] initWithContentViewController: self.popupMainViewController];
    }
    
// set size
# ifdef  __IPHONE_7_0
    self.popupMainViewController.preferredContentSize = CGSizeMake(341, 350);
# else
    self.popupMainViewController.contentSizeForViewInPopover = CGSizeMake(341, 350);
# endif
    
    if (!self.popoverViewController.popoverVisible){
        [self.popoverViewController presentPopoverFromRect : button.frame
                                                    inView : self.view
                                  permittedArrowDirections : NO
                                                  animated : YES];
    }
}

# pragma mark - PopupMainViewControllerDelegate
/// 適当なデリゲートのメソッドを作成
- (void)popupMainViewController:(PopupMainViewController *)popupMainViewController dismiss:(NSObject)dismiss {
    // ここで、popoverViewControllerをcloseする処理を行います
    [self.popoverViewController dismissPopoverAnimated:true];
}

上記が今までのpopoverの処理の流れでした。

そして

 変更された内容

UIPopoverController
↓↓↓↓
UIPopoverPresentationController
UIViewController の中に popoverPresentationController が取り組まれたようですね。
ということで、基本的な設定は以下の通りです。

HogeNewPopOver.m

// 使わなくなります。@property (retain, nonatomic) UIPopoverController *popoverViewController;
@property (retain, nonatomic) PopupMainViewController *popupMainViewController;

/// これまでの書き方 (PopupMainViewController:ポップアップで表示されるViewController)
- (void)newPopoverAction:(UIButton *)button {

// ポップオーバーしたいViewController の準備
// PopupMainViewController の生成
    if(!self.popupMainViewController){
        self.popupMainViewController = [[PopupMainViewController alloc] initWithNibName:@"PopupMainViewController" bundle:nil];
        self.popupMainViewController.delegate = self;
    }
    
    self.popupMainViewController.delegate = self;
    //define use of popover
    self.popupMainViewController.modalPresentationStyle = UIModalPresentationPopover;
    //set size
    self.popupMainViewController.preferredContentSize = CGSizeMake(341, 350);
    //set origin
    self.popupMainViewController.popoverPresentationController.sourceView = self.view;
    self.popupMainViewController.popoverPresentationController.sourceRect = button.frame;
    //set arrow direction
    self.popupMainViewController.popoverPresentationController.permittedArrowDirections = NO;
    //set delegate
    self.popupMainViewController.popoverPresentationController.delegate = self;
    //present
    [self presentViewController:self.popupMainViewController animated:YES completion:nil];

}

# pragma mark - PopupMainViewControllerDelegate
/// 適当なデリゲートのメソッドを作成
- (void)popupMainViewController:(PopupMainViewController *)popupMainViewController dismiss:(NSObject)dismiss {
    // ここで、ポップオーバーしたViewController を closeする処理を行います
    [self.popupMainViewController dismissViewControllerAnimated:YES completion:nil];
}

書き方としては、とてもわかりやすいののかな?

用語について

  • ポップオーバーについて

あるコンテンツの上に被せて表示する、一時的なビューのことを指します。
ポップオーバー表示中は、他のビューに対する操作は基本的にできません。出現場所は矢印によって表示されることもありますが必要がない時には矢印を表示しないこともできます。
ポップオーバーにはモーダル型、非モーダル型があり、後者の場合はキャンセルボタンの他、ポップオーバー外のエリアをタップすることによって消すことができます。ポップオーバー内にはナビバーやテーブルビューなど、様々な要素を含めることができ、大画面での使用に適しています。iPhone の場合はポップオーバーではなくモーダル表示が好まれますよ。

まとめ

UIPopoverController
↓↓↓↓
UIPopoverPresentationController
に変更されました

また、UIViewController に popoverPresentationController が追加された。

書き方がシンプルになった。

バージョンアップの際、警告になってるよっていう方はこのように変更してみてはいかがでしょうか?

それではまた!!

制作チーム:サンストライプ

http://sunstripe.main.jp/
(月1WEBコンテンツをリリースして便利な世の中を作っていくぞ!!ボランティアプログラマー/デザイナー/イラストレーター/その他クリエイター声優募集中!!)

地域情報 THEメディア

THE メディア 地域活性化をテーマに様々なリリース情報も含め、記事をお届けしてます!!
https://the.themedia.jp/

ゼロからはじめる演劇ワークショップ

多様化の時代に向けて他者理解を鍛える

ただいま、コロナの影響で中止中 6月ごろまで延期
https://workshop.themedia.jp/

プログラミングワークショップ・ウェブ塾の開講!!!

様々なテーマでプログラミングに囚われずに取り組んでいきます。
詳しくはこちら ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
プログラミングサロン 月1だけのプログラミング学習塾

青空プログラミング

まずはリフレッシュして考えてみようIoTの未来の社会を考えながらも社会問題などにも取り組む活動しています。

以下を用いています。
actcoin:SDGsを知るきっかけとなりました社会貢献の見える化を実現しているアプリケーションサービス
SDGs:持続可能な開発目標
Zoom:オンライン会議(Zoomを用いて会議をしたりしてます)

協力応援 / 支援者の集い

トラストヒューマン

http://trusthuman.co.jp/
私たちは何よりも信頼、人と考えてます。

「コンサルティング」と「クリエイティブ」の両角度から「人材戦略パートナー」としてトータル的にサポートします!!

ネリム

https://nerim.co.jp/
配信事業などを映像コンテンツなどのサポートしております。

ビヨンドXプロジェクト

ビヨンドXプロジェクト全体会議

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?