Skip to content

Instantly share code, notes, and snippets.

@hfm
Created July 8, 2012 17:25
Show Gist options
  • Save hfm/3071913 to your computer and use it in GitHub Desktop.
Save hfm/3071913 to your computer and use it in GitHub Desktop.
UIActionSheetのキャンセルボタンを最下部に設定する(ユニバーサル仕様)
// UIActionSheetクラスを作成する
// cancelButtonとdestructiveButtonはどちらかをnilにしておく
UIActionSheet *sheet;
sheet = [[UIActionSheet alloc] initWithTitle:@"写真選択"
delegate:self
cancelButtonTitle:@"キャンセル"
destructiveButtonTitle:nil
otherButtonTitles:@"カメラ", @"ライブラリ", nil];
[sheet showInView:self.view];
// UIActionSheetクラスを作成する
// cancelButtonとdestructiveButtonはnilにしておく
UIActionSheet *sheet;
sheet = [[UIActionSheet alloc] initWithTitle:@"写真選択"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:@"カメラ", @"ライブラリ", nil];
// キャンセルボタンをアクションシートの最下に設定する
[sheet addButtonWithTitle:@"キャンセル"];
[sheet setCancelButtonIndex:[sheet numberOfButtons] - 1];
[sheet showInView:self.view];
@hfm
Copy link
Author

hfm commented Jul 8, 2012

初期化オーバーライドにcancelButtonTitle:@"キャンセル"と設定しておけばiPhoneではキャンセルボタンが表示されるが、iPadではcancelButtonは表示されない。

iPadにおけるキャンセルはアクションシートの領域外をタッチすることである。

そこで、上記のようにaddButtonWithTitle:メソッドでボタンを追加し、setCancelButtonIndex:メソッドで追加したボタンにキャンセルボタンの役割を与えることで、iPhone/iPadで同じように表示できる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment