關於自定義AlertView背景的方法收集


從網上收集了一些自定義AlertView背景的方法,匯總一下以便有需要時使用。

UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Atention" message:@"我是中國人!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil] autorelease];
[theAlert show];

// undocument API UIAlertView類頭文件里面帶 “ _”的成員是可以通過 valueforkey來引用的。但這些都是不公開的,私有方法
UILabel *theTitle = [theAlert valueForKey:@"_titleLabel"];
[theTitle setTextColor:[UIColor greenColor]];

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

/* 第一種自定義方法
//
UIImageView *imgv = [theAlert valueForKey:@"_backgroundImageView"];
imgv.image = [UIImage imageNamed:@"loveChina.png"];
*/
//

/* 第二種自定義方法,因有過期屬性的使用,所以新版iOS中無效
//
// undocument API
UIImage *theImage = [UIImage imageNamed:@"loveChina.png"];
theImage = [theImage stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
CGSize theSize = [theAlert frame].size;

UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
theAlert.layer.contents = (id)[theImage CGImage]; // iOS4.0開始不支持contents屬性
*/
//

/* 第三種自定義方法
//遍歷theAlert對象的子view,獲取其UIImageView視圖
for (UIView *v in [theAlert subviews]) {
if ([v isKindOfClass:[UIImageView class]]) {
UIImage *theImage = [UIImage imageNamed:@"loveChina.png"];
((UIImageView *)v).image = theImage;
}
}
*/

/* 第四種自定義方法 */
UIView *additionBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, theAlert.frame.size.width-30, theAlert.frame.size.height-20)];
additionBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"loveChina.png"]];
#if TARGET_IPHONE_SIMULATOR
[theAlert insertSubview:additionBackgroundView atIndex:1];
#else
[theAlert insertSubview:additionBackgroundView atIndex:0];
#endif

[additionBackgroundView release];

第五種自定義代碼:

 1 UIAlertView *theAlert = [[[UIAlertView alloc] initWithTitle:@"Atention" message:@"我是中國人!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil] autorelease];
2 UIImage *alertImage = [UIImage imageNamed:@"loveChina.png"];
3 UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:alertImage];
4 backgroundImageView.frame = CGRectMake(0, 0, 282, 160);
5 backgroundImageView.contentMode = UIViewContentModeScaleToFill;
6 [theAlert addSubview:backgroundImageView];
7 [theAlert sendSubviewToBack:backgroundImageView];
8
9 [theAlert show];
10 [theAlert release];

運行效果如圖:

第六種方式:使用一個定義擴展類JKCustomAlert (網上有下載)。
調用代碼:

UIImage *backgroundImage = [UIImage imageNamed:@"Splatter.png"];
alert = [[JKCustomAlert alloc] initWithImage:backgroundImage text:NSLocalizedString(@"game over", nil)];
[alert show];

運行效果圖:


為了在iOS4.2以上也有效,需增加些代碼來手動隱藏原AlertView的背景視圖:修改layoutSubviews方法

1 - (void) layoutSubviews {
2 for (UIView *v in [self subviews]) {
3 if ([v class] == [UIImageView class]) {
4 [v setHidden:YES];
5 }
6 }
7
8 //原來的代碼繼續
9 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM