Iphone 如何在UIAlertView中仅对齐标题

Iphone 如何在UIAlertView中仅对齐标题,iphone,objective-c,cocoa-touch,uiimageview,Iphone,Objective C,Cocoa Touch,Uiimageview,我只想将UIAlertView中的标题居中对齐,而消息应该是左对齐 目前我正在做 ((UILabel*)[[alert subviews] objectAtIndex:1]).textAlignment = NSTextAlignmentCenter; 但是它也会使消息居中对齐。我不会尝试修改UIAlertView的默认标签,而是应该创建一个新的UILabel并将其添加为子视图 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"

我只想将UIAlertView中的标题居中对齐,而消息应该是左对齐

目前我正在做

((UILabel*)[[alert subviews] objectAtIndex:1]).textAlignment = NSTextAlignmentCenter;

但是它也会使消息居中对齐。

我不会尝试修改UIAlertView的默认标签,而是应该创建一个新的UILabel并将其添加为子视图

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Centered Title" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12.0, 24.0, 250.0, 80.0)];
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.text = @"Here is an example of a left aligned label in a UIAlertView!";
[alert addSubview:label];
[alert show];

这是访问子视图的一种非常危险的方式。如果您真的想自定义alertview的外观,您可能需要创建自己的自定义alertview。试试教程。