終於到了周末了,可以抽點時間寫自己的第一篇原創文章了,這是本人第一次寫文章,請大家多捧場,寫得不好請見諒,當然有任何問題也可以給我留言,非常樂意和大家交流學習。
支持原創,轉載請注明出處。
其實這個例子是從網上找到的,自己研究了一下,終於搞明白了如何自定義UIAlertView,將其修改之后為大家分享。

@interface CustomAlertView : UIAlertView @end
@implementation CustomAlertView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } -(void)layoutSubviews{//這個是重點!!重新定義UIAlertView中的各種控件,包括UILabel,UIButton等 for (UIView *v in self.subviews) {//遍歷UIAlertView中的所有控件(UIView),再將它們重新設置 if ([v isKindOfClass:[UILabel class]]) {//設置Label UILabel *label = (UILabel *)v; if ([label.text isEqualToString:self.title]) { label.font = [UIFont boldSystemFontOfSize:40]; label.numberOfLines = 0; label.lineBreakMode = UILineBreakModeWordWrap; label.textColor = [UIColor redColor]; label.backgroundColor = [UIColor clearColor]; label.textAlignment = UITextAlignmentCenter; label.shadowColor = [UIColor blackColor]; label.shadowOffset = CGSizeMake(0, -1);//設置陰影 [label sizeToFit];//文字大小自動適應 label.center = CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.2); }else{ label.font = [UIFont boldSystemFontOfSize:20]; label.numberOfLines = 0; label.lineBreakMode = UILineBreakModeWordWrap; label.textColor = [UIColor yellowColor]; label.backgroundColor = [UIColor clearColor]; label.textAlignment = UITextAlignmentCenter; label.shadowColor = [UIColor blackColor]; label.shadowOffset = CGSizeMake(0, -1); label.center = CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.3); } } if ([v isKindOfClass:NSClassFromString(@"UIAlertButton")]) {//設置Button UIButton *button = (UIButton *)v; UIImage *image = nil; if (button.tag == 1) {//給button的標簽賦一個值,以便區分左右兩個Button,對其進行更詳細的設置。 image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", @"red"]]; }else{ image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", @"black"]]; } image = [image stretchableImageWithLeftCapWidth:(int)(image.size.width+1)>>1 topCapHeight:0];//這句話的作用是:將image圖片的內容拉伸,而邊角不拉伸。>>是右移運算符,感興趣的同學可以去百度一下,這里不需要知道原理,了解作用就行了 button.titleLabel.font = [UIFont boldSystemFontOfSize:18]; button.titleLabel.minimumFontSize = 10; button.titleLabel.textAlignment = UITextAlignmentCenter; button.titleLabel.shadowOffset = CGSizeMake(0, -1); button.backgroundColor = [UIColor clearColor]; [button setBackgroundImage:image forState:UIControlStateNormal]; if (button.tag == 1) button.center = CGPointMake(self.bounds.size.width*0.25, self.bounds.size.height*0.75); else button.center = CGPointMake(self.bounds.size.width*0.75, self.bounds.size.height*0.5); // [button setTitleColor:kAlertViewButtonTextColor forState:UIControlStateNormal]; [button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal]; } } } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code UIImageView *pic = [[UIImageView alloc]initWithFrame:CGRectMake(230, 45, 60, 60)]; [pic setImage:[UIImage imageNamed:@"LittleWhitePig.png"]]; [self addSubview:pic]; [pic release]; } - (void)show { [super show]; self.bounds = CGRectMake(0, 100, 320, 300);//更改整個UIAlertView的框架 } @end
代碼很簡單,重點-(void)layoutSubviews方法,這個方法是設置系統的控件的,設置思想就是先遍歷所有的空間,再判斷這是什么控件,if ([v isKindOfClass:NSClassFromString(@"UIAlertButton")])這個可以判斷是不是UIAlertButton的子類。
接下來就只需要在ViewController里面創建這個類的對象就行了,再show就行了
- (IBAction)showAlertView:(id)sender { CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"小" message:@"白豬" delegate:nil cancelButtonTitle:@"帥" otherButtonTitles:@"很帥", nil]; [alert show];
然后附上效果圖:
有什么問題請留言,歡迎指正