トップ «前の日記(2012-05-26) 最新 次の日記(2012-05-28)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|

2012-05-27 [iOS]アラートのカスタマイズ

UIAlertViewについては、もっとカスタマイズできるようにして欲しいが、何故かあまり手をつけられていない。Blocks対応もまだなので、もしかしたら、何か大きな変更を考えているのかもしれない。

アラートで画像等を表示したくなった。そも最も簡単な方法が画像のビューを追加する方法だが、追加するだけだとアラート全体のサイズが変更されないので、追加した画像と、元々のボタンが重なる等、不具合が発生する。そこで、自分が追加した画像のサイズから、アラート全体とボタンの位置を調整する事になる。

- (IBAction)alertImage:(id)sender
{
    /* 画像を用意 */
    UIImage *image = [UIImage imageNamed:@"likeness.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(10.0, 80.0, 100.0, 100.0);
    
    UIAlertView *alertView = [[UIAlertView alloc] init];
    alertView.delegate = self;
    alertView.title = @"Alert Image";
    alertView.message = @"a likeness";
    [alertView addSubview:imageView]; /* 画像を追加 */
    [alertView addButtonWithTitle:@"NO"];
    [alertView addButtonWithTitle:@"YES"];
    alertView.cancelButtonIndex = 0;
    [alertView show];
}
 
/* アラートのボタン押下に対応 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    DBGMSG(@"%s, buttonIndex(%d)", __func__, (int)buttonIndex);
}
 
/* アラートのサイズを調整する */
- (void)willPresentAlertView:(UIAlertView *)alertView
{
    CGRect  frame = alertView.frame;
    frame.origin.y -= 50.0; /* 追加した分、縦軸の座標を調整 */
    frame.size.height += 100.0; /* 追加した分、縦の長さを増やす */
    alertView.frame = frame;
    
    for (UIView* view in alertView.subviews) {
        frame = view.frame;
        if (frame.origin.y > 80) { /* 追加したビューより下 */
            frame.origin.y += 100; /* 追加した分、縦軸の座標を調整 */
            view.frame = frame;
        }
    }
}

上手く表示されたが、何度もテストして位置の調整が必要そうで、あまり、お勧めできるもので内容に思えるので、次回は違う方法を模索してみたい。

実行

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/ios/Dialog - GitHub

_ 関連情報

[iOS] UIAlertView 上に UIProgressView を載せる [2] キャンセルボタン表示
『Cocoaの日々』いつも参考にさせていただいています。ありがとう!助かります。

トップ «前の日記(2012-05-26) 最新 次の日記(2012-05-28)» 編集