前回のコメントで 335gさんから UIAlertView のサイズ変更とボタン位置の変更ができることを教えてもらったので試してみた。
サンプル
前回のコードでキャンセルボタンをそのまま表示するとこんな風になっていた。
今回の修正を加えるとこうなる。
実装
方法は UIAlertViewDelegate のメソッドを使う。
- (void)willPresentAlertView:(UIAlertView *)alertView
{
// (1) サイズと位置を変更する
CGRect frame = alertView.frame;
frame.origin.y -= 15;
frame.size.height += 30;
alertView.frame = frame;
// ボタン位置を変更する
for (UIView* view in alertView.subviews) {
frame = view.frame;
if (frame.origin.y > 80) {
frame.origin.y += 25;
view.frame = frame;
}
}
}UAlertViewのデリゲートを設定しておくと表示前に -willPresentAlertView: が呼ばれる。この引数に UIAlertView が渡ってくるので frameを変更すればサイズと位置の変更ができる。またサブビュー内にキャンセルボタンがあるのでこの表示位置を下へ移動させれば重ならなくなる。今回は UIProgressView の表示位置より下のものをすべて 25ピクセル下へずらすようにした。ここはボタンのクラスを狙い撃ちすることもできる。UIAlertView.subviews を見るとこんな構成になっていた。[74442:207] UIImageView [74442:207] UILabel [74442:207] UILabel [74442:207] UIThreePartButton [74442:207] UIProgressViewUIThreePartButton がキャンセルボタンなのでこんな感じで判定できる。
if ([view isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {UIThreePartButton はプライベートなクラスなようなので名前からクラスのインスタンスを取得して比較に使っている。isKindOfClass:[UIButton class] では駄目だったので UIButton のサブクラスではないようだ。ソースコード
GitHub からどうぞ。
ProgressBarOnAlertView at 2010-12-19 from xcatsan/iOS-Sample-Code - GitHub




匿名
2011年4月7日 10:11
ProgressBarOnAlertViewとても参考になりました。
UIAlertViewのサイズ変更ですが、messageに空行を追加して高さを調整する方法もあります。
トリッキーな方法なのでなにか副作用あるかもしれませんが。。
self.progressAlertView = [[[UIAlertView alloc] initWithTitle:@"Title"
message: [NSString stringWithFormat:@"%@\n\n", msg]
delegate: self
cancelButtonTitle: @"Cancel"
otherButtonTitles: nil] autorelease];
匿名
2011年4月7日 10:11
ProgressBarOnAlertViewとても参考になりました。
UIAlertViewのサイズ変更ですが、messageに空行を追加して高さを調整する方法もあります。
トリッキーな方法なのでなにか副作用あるかもしれませんが。。
self.progressAlertView = [[[UIAlertView alloc] initWithTitle:@"Title"
message: [NSString stringWithFormat:@"%@\n\n", msg]
delegate: self
cancelButtonTitle: @"Cancel"
otherButtonTitles: nil] autorelease];
xcatsan says:
2011年4月20日 11:40
なるほど。これはお手軽ですね。
xcatsan says:
2011年4月20日 11:40
なるほど。これはお手軽ですね。