【iOS8以上版本舌剂,蘋果推薦使用UIAlertController替代UIAlertView 跟 UIActionSheet】
網(wǎng)上有說以前可以通過-(void)willPresentAlertView:(UIAlertView *)alertView來修改alertView的subview的代碼,但是在iOS7以上的系統(tǒng)沒有作用菩暗。
而且通過-(void)willPresentAlertView:(UIAlertView *)alertView和-(void)didPresentAlertView:(UIAlertView *)alertView獲取到的alertView.frame 都是 {{0,0}鞍匾,{0瞳氓,0}}。
我目前找到兩個方法實現(xiàn)UIAlertView內(nèi)容左對齊:
一個是自定義UIAlertView:
github上正好有一個已經(jīng)寫好的自定義UIAlertView類(CustomIOSAlertView)靴庆。
https://github.com/wimagguc/ios-custom-alertview
直接導(dǎo)入CustomIOSAlertView類就可以用了。
#import "CustomIOSAlertView.h"
@interface ViewController ()
@end
@implementation ViewController
//注意怒医,對話框不要創(chuàng)建在viewDidLoad里面炉抒,否則不顯示,介個折騰了我好久淚稚叹!
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self launchDialog];
}
- (void)launchDialog
{
CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
[alertView setContainerView:[self createDemoView]];
[alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"取消", @"更新", nil]];
[alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
if (buttonIndex == 1) {
NSLog(@"確定");
} else if(buttonIndex == 0){
NSLog(@"取消");
}
[alertView close];
}];
[alertView setUseMotionEffects:true];
[alertView show];
}
- (UIView *)createDemoView
{
float textWidth = 260;
float textMargin = 10;
UILabel *titleLabel = [[UILabel alloc]init];
titleLabel.font = [UIFont systemFontOfSize:18];
titleLabel.textColor = [UIColor blackColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.lineBreakMode =NSLineBreakByWordWrapping;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = @"新版本檢查";
titleLabel.frame = CGRectMake(0, textMargin, textMargin * 2 + textWidth, 40);
NSString *message =? @"1焰薄、增加白菜黨等特色標簽增加白菜黨等特色標簽增加白菜黨等特色標簽篩選\n2、增加頻道熱度排行\(zhòng)n3扒袖、增加夜間模式\n4蛤奥、Material design風(fēng)格優(yōu)化\n5、滑動返回優(yōu)化\n6僚稿、其他bug修復(fù)";
UIFont *textFont = [UIFont systemFontOfSize:15];
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = textFont;
CGSize maxSize = CGSizeMake(textWidth-textMargin*2, MAXFLOAT);
CGSize size = [message boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(textMargin,CGRectGetMaxY(titleLabel.frame) + textMargin,textWidth, size.height)];
textLabel.font = textFont;
textLabel.textColor = [UIColor blackColor];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.lineBreakMode =NSLineBreakByWordWrapping;
textLabel.numberOfLines =0;
textLabel.textAlignment =NSTextAlignmentLeft;
textLabel.text = message;
UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, textWidth + textMargin * 2,CGRectGetMaxY(textLabel.frame)+textMargin)];
[demoView addSubview:titleLabel];
[demoView addSubview:textLabel];
NSLog(@"%@",demoView);
return demoView;
}
第二中針對alertView的方法凡桥,由于UIalertView在iOS8中已經(jīng)不推薦使用,略蚀同。