iOS 12系统版本以后,原本用UIAlertController去遍历图层获取UILabel的方法失效,原因是UILabel的图层位置改变了
iOS 12 之前:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示!" message:tips preferredStyle:UIAlertControllerStyleAlert];
UIView *subView1 = alert.view.subviews[0];
UIView *subView2 = subView1.subviews[0];
UIView *subView3 = subView2.subviews[0];
UIView *subView4 = subView3.subviews[0];
UIView *subView5 = subView4.subviews[0];
//分别拿到title 和 message 可以分别设置他们的对齐属性
UILabel *title = subView5.subviews[0];
UILabel *message = subView5.subviews[1];
message.textAlignment = NSTextAlignmentLeft;
iOS 12 之后:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示!" message:tips preferredStyle:UIAlertControllerStyleAlert];
UIView *subView1 = alert.view.subviews[0];
UIView *subView2 = subView1.subviews[0];
UIView *subView3 = subView2.subviews[0];
UIView *subView4 = subView3.subviews[0];
UIView *subView5 = subView4.subviews[0];
//分别拿到title 和 message 可以分别设置他们的对齐属性
NSArray * viewArray = subView5.subviews;
int index = [self getIndex];
UILabel *title = viewArray[index + 0];
UILabel *message = viewArray[index + 1];
message.textAlignment = NSTextAlignmentLeft;
- (int) getIndex{
NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
int index;
if ([phoneVersion doubleValue] >= 12.0) {
index = 1;
} else {
index = 0;
}
return index;
}