LoginSignup
16
15

More than 5 years have passed since last update.

iOS8でUIActionSheetに追加したUIDatePickerなどのviewが表示されない時の対処法

Last updated at Posted at 2014-08-28

Xcode6 beta5で検証。

UIActionSheetにUIDatePickerやUIToolbarをaddSubviewしているものが、表示されない!

actionsheet.png

こんな感じで。

UIActionSheetはiOS8からdeprecatedです

UIActionSheetがdeprecatedになったので、代替としてiOS8ではUIAlertControllerを利用したほうがよいです。

iOS8でのアクションシート:廃止されるUIActionSheetと推奨されるUIAlertController

iOS7以前とiOS8以降で切り分けが必要。

ただ現在(2014/08/28、Xcode6 beta5)のところ、viewなどを追加していないUIActionSheet自体は問題なく表示できているため、deprecatedでの影響ではない。

UIActionSheetのドキュメント

Appleの公式ドキュメントから、そもそもはUIActionSheetにviewのせるなよってiOS8以前から書かれてたようです。

UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.
https://developer.apple.com/library/ios/documentation/uikit/reference/uiactionsheet_class/Reference/Reference.html

となると、UIAlertControllerやUIActionSheetをバージョンによって切り替えるなどの実装をするより、自前でviewを作って実装するしかないかと。

代替案

とはいえ、UIActionSheetを独自viewで代替実装となると、全体のviewをブロックしてアニメーションさせて表示させたり、タップで解除したりなどそれなりに手間がかかりそう。

でいいいもの見つけてきました!

RMDateSelectionViewController
https://github.com/CooperRS/RMDateSelectionViewController

実装はかなりシンプル

RMDateSelectionViewController *dateSelectionViewController = [RMDateSelectionViewController dateSelectionController];
dateSelectionViewController.hideNowButton = YES;

dateSelectionViewController.datePicker.datePickerMode = UIDatePickerModeDate;
[dateSelectionViewController showWithSelectionHandler:^(RMDateSelectionViewController *vc, NSDate *aDate) {
    //選択したとき
  } andCancelHandler:^(RMDateSelectionViewController *vc) {
    //キャンセルしたとき
}];

blocksで実装できます。

are.png

簡単かつアニメーションまでいい感じですね!

16
15
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
16
15