iPhone開発のネタ帳: UIPopoverController に UIPickerView をいれる
iPad から追加された部品の一つに、UIPopoverController がある。
iPadが出るまではNDAのためブログにも書けなかったが、今はApple公式ページにReferenceが公開されている。
自作アプリでは、iPhone版では設定画面のためにNavigationControllerを使って全画面で設定を行っているが、iPadからは基本的に全画面の遷移はなくなり、必要に応じてPopupを使うことになっている。(iPad HIG: Human Interface Guideline 参照)
このため、iPhone版で用意している設定画面を UIPopoverControllerで用意することとした。
UIPopoverController は中にどんなUIViewControllerでもいれることができるらしい。
– (id)initWithContentViewController:(UIViewController *)viewController
でPopoverを作成する。
実際のコードは下記のようなもの。NavigationController には UITableView をいれている。
-(IBAction)settingsButtonPressed:(id)sender { if (!mPopoverController) { NSString *settings = NSLocalizedString(@"Settings", @"Settings"); SettingViewController *settingVC = [[SettingViewController alloc]initWithStyle:UITableViewStyleGrouped]; UINavigationController* theNavController = [[UINavigationController alloc] initWithRootViewController:settingVC]; theNavController.navigationBar.topItem.title = settings; [theNavController setNavigationBarHidden:NO]; [theNavController setToolbarHidden:NO]; mPopoverController = [[UIPopoverController alloc]initWithContentViewController:theNavController]; mPopoverController.delegate = self; [settingVC release]; [theNavController release]; } if(!mPopoverController.popoverVisible) { // visible でない場合 [mPopoverController presentPopoverFromBarButtonItem:self.navigationItem.leftBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } }
設定画面の中で、数値を選ぶ必要があったので、SettingViewControllerから、UIPickerViewを呼び出すようにした。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger section = [indexPath section]; switch (section) { case 0: { [self.navigationController pushViewController:mMondaisuPickerVC animated:YES]; break; } default: break; } [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
分からないこと:
UIPopoverController が画面一杯まで表示されてしまう。必要な大きさだけ表示したいのだが。UIPickerView を表示すると特に無駄スペースが多い。
UIPickerView に遷移するとPopoverの色が黒くなるのだがこれはそういうものなのだろうか。
関連記事
-
-
[WatchKit] Apple WatchアプリをRejectされた話
Apple Watch はいつの間にか電池がかなり減っていることが多く、いつどのように減っているか知
-
-
GTD用にOmniFocusを購入
半年くらいMacBookとiPhoneでOmniFocusを使っている。 なかなか良いので、紹介して
-
-
iPhone/iPad で音楽
iPhone/iPad で音楽制作的な本を書店でよく見かけるので、リストアップしてみる。 どれも面白
-
-
Cocoa Design Patterns
いまさらながら最近デザインパターンにはまっており、Cocoaでどんなデザインパターンが使われているの
-
-
Parse から Heroku, AWSへの移行ガイド
2017年1月28日にParseのサービスが終了するという衝撃的なニュース(Moving On)
-
-
[WatchKit] WatchSim 買ってみた
WatchSim Danny Keogan posted with iT
-
-
iOS9から[NSLocale preferredLanguages] の出力が変更された
以前ツイートした内容を自分で忘れていて検索する羽目になったのでブログにも書いておく。iOS9から、
-
-
久々に Reject をくらった話
稼働日カウントダウンという昔作ったアプリにAppStoreのレビューのコメントでリクエストがあった。
-
-
EverLearn 1.9.0 に音声認識機能を追加しました
EverLearn 1.9.0 にて音声認識機能を追加しました。ホーム画面から、マイクボタンを押して
-
-
[iOS SDK] Pebble腕時計対応iOSアプリを作る
英単語学習アプリ WordLearnをリリースしましたに書いたけれども今Pebble腕時計対応iOS
Comment
NavigationControllerのviewDidLoadあたりで、下のように設定すると必要な大きさだけ表示可能です。
self.contentSizeForViewInPopover = CGSizeMake(width, height);
わざわざ書き込みありがとうございます。
後ほど試してみます。