38

UIAlertView の背景色を変更したいのですが、これには color 属性がないようです。

4

12 に答える 12

34

AlertView の背景は画像で、この画像を変更できます

UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Atention"
   message: @"YOUR MESSAGE HERE", nil)
   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];

   [theAlert show];

   UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];
   [theTitle setTextColor:[UIColor redColor]];

   UILabel *theBody = [theAlert valueForKey:@"_bodyTextLabel"];
   [theBody setTextColor:[UIColor blueColor]];

   UIImage *theImage = [UIImage imageNamed:@"Background.png"];    
   theImage = [theImage stretchableImageWithLeftCapWidth:16 topCapHeight:16];
   CGSize theSize = [theAlert frame].size;

   UIGraphicsBeginImageContext(theSize);    
   [theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];    
   theImage = UIGraphicsGetImageFromCurrentImageContext();    
   UIGraphicsEndImageContext();

   [[theAlert layer] setContents:[theImage CGImage]];
于 2009-05-19T15:08:32.097 に答える
13

ほぼ同じ回避策も見つけましたが、私の場合はうまく機能します。酸素を使用すると、画面上の結果が悪く、コンテキストがぼやけます。UIAlertView をサブクラス化するのではなく、以下を実装します。

- (void)willPresentAlertView:(UIAlertView *)alertView;

だから...コピーして貼り付けてください

- (void)willPresentAlertView:(UIAlertView *)alertView 
{
    blah blah blah...
    [[alertView layer] setContents:(id)theImage.CGImage];  // to avoid the warning
}
于 2009-09-25T18:26:58.463 に答える
10

最近これが必要になり、UIAlertView をサブクラス化することになりました。興味のある人のためのコードは次のとおりです。

http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color

于 2010-01-08T13:52:16.813 に答える
7

さて、私は別の方法で問題を解決しました。背景画像を含む UIImageView を検索し、画像を変更しました。

...まあ、表示を変更するための最良の解決策ではないかもしれません...

@implementation CustomAlertView

- (void)show {
    [super show];

    for (UIView *view in self.subviews) {
        NSLog(@"%@ with %i children", view, [view.subviews count]);
        // change the background image
        if ([view isKindOfClass:[UIImageView class]]) {
            UIImage *theImage = [UIImage imageNamed:@"password entry bg - default.png"];    
            ((UIImageView *)view).image = [theImage resizableImageWithCapInsets:UIEdgeInsetsMake(49, 8, 8, 8)];
        }
    }
}

@end
于 2011-12-28T10:08:56.033 に答える
4

ブロックを利用し、TweetBot をモデルにした、より最近のソリューションを次に示します。

https://github.com/Arrived/BlockAlertsAnd-ActionSheets

于 2012-02-23T06:42:45.420 に答える
3

これは可能なことではありません。

独自のアラートタイプのビュー(アニメーションの提供などを含む)を作成するか、Appleが提供するデフォルトのUIAlertViewを使用する必要があります。

Appleは、UIAlertViewの色などを公開しない傾向があるため、UIはすべてのアプリケーションで共通の感覚を持っています。アプリごとに色を変更すると、ユーザーが混乱する可能性があります。

于 2009-01-24T18:40:51.397 に答える
2

アプリケーションに QuartzCore フレームワークを追加し、ソース ファイルに #import "QuartzCore/CALayer.h" を含めます。

于 2009-12-14T13:02:28.617 に答える
1

これは私にとって最も堅牢なソリューションのように見えます

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/

于 2013-02-07T09:22:30.343 に答える
1

私は最近、GRAlertViewを見つけました。これにより、少なくとも私が好きな簡単な方法でUIAlertViewを調整できます。ここにあります:https ://github.com/goncz9/GRAlertView

于 2013-03-20T20:45:56.700 に答える
1

これを使用する必要があります:

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"You are Select Row of" message:@"Human" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        alert.backgroundColor=[UIColor redColor];
        [alert show];
        [alert release];

UIAlertView の背景色を変更します。

于 2012-04-23T12:34:02.013 に答える
0

このコードを使用するだけで、

 UIImage *theImage = [UIImage imageNamed:@"imageName.png"];
 UIView *view=[alert valueForKey:@"_backgroundImageView"];
 //Set frame according to adustable
  UIImageView *image=[[UIImageView alloc] initWithImage:theImage];
 [image setFrame:CGRectMake(0, 0, 280, 130)];
 [view addSubview:image];
于 2013-02-20T07:29:25.100 に答える
-3

これを行うための公開されたメソッドまたはプロパティがあるとは思いません。UIAlertView の backgroundColor を (UIView として) 設定すると、警告ビューの背後に色付きの長方形の背景が配置されるだけです。

この色を変更することは、おそらく iPhone の一般的なインターフェイスに反するので、推奨される動作ではないと思います (Mac OS X で警告ビューの背景色を変更することができないのと同じように)。おすすめされた)。

于 2009-01-24T17:09:53.813 に答える