[英]Changing the background color of a UIAlertView?
我想更改UIAlertView的背景顏色,但這似乎沒有顏色屬性。
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]];
我也發現了一個幾乎相同的解決方法,在我看來,它的效果更好。 使用oxigen方式我發現屏幕上的結果很差,上下文變得模糊。 而不是子類化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”。
你必須使用這個:
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的背景顏色。
我最近發現了GRAlertView,它允許你以一種簡單的方式定制UIAlertView,至少我喜歡。 你可以在這里找到它: https : //github.com/goncz9/GRAlertView
只需使用此代碼,
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中警報視圖的背景顏色一樣)推薦的)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.