UIAlertView の背景色を変更したいのですが、これには color 属性がないようです。
12 に答える
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]];
ほぼ同じ回避策も見つけましたが、私の場合はうまく機能します。酸素を使用すると、画面上の結果が悪く、コンテキストがぼやけます。UIAlertView をサブクラス化するのではなく、以下を実装します。
- (void)willPresentAlertView:(UIAlertView *)alertView;
だから...コピーして貼り付けてください
- (void)willPresentAlertView:(UIAlertView *)alertView
{
blah blah blah...
[[alertView layer] setContents:(id)theImage.CGImage]; // to avoid the warning
}
最近これが必要になり、UIAlertView をサブクラス化することになりました。興味のある人のためのコードは次のとおりです。
http://kwigbo.com/post/318396305/iphone-sdk-custom-uialertview-background-color
さて、私は別の方法で問題を解決しました。背景画像を含む 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
ブロックを利用し、TweetBot をモデルにした、より最近のソリューションを次に示します。
これは可能なことではありません。
独自のアラートタイプのビュー(アニメーションの提供などを含む)を作成するか、Appleが提供するデフォルトのUIAlertViewを使用する必要があります。
Appleは、UIAlertViewの色などを公開しない傾向があるため、UIはすべてのアプリケーションで共通の感覚を持っています。アプリごとに色を変更すると、ユーザーが混乱する可能性があります。
アプリケーションに QuartzCore フレームワークを追加し、ソース ファイルに #import "QuartzCore/CALayer.h" を含めます。
これは私にとって最も堅牢なソリューションのように見えます
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/
私は最近、GRAlertViewを見つけました。これにより、少なくとも私が好きな簡単な方法でUIAlertViewを調整できます。ここにあります:https ://github.com/goncz9/GRAlertView
これを使用する必要があります:
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 の背景色を変更します。
このコードを使用するだけで、
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];
これを行うための公開されたメソッドまたはプロパティがあるとは思いません。UIAlertView の backgroundColor を (UIView として) 設定すると、警告ビューの背後に色付きの長方形の背景が配置されるだけです。
この色を変更することは、おそらく iPhone の一般的なインターフェイスに反するので、推奨される動作ではないと思います (Mac OS X で警告ビューの背景色を変更することができないのと同じように)。おすすめされた)。