此解決方案確實使用私有API /屬性。根據我的研究和經驗,這是我知道您可以自定義UIAlertController的唯一方法。如果您查看公共標題,則UIAlertContoller幾乎沒有自定義空間。然而,在iOS 8中啓動UIAlertController之後,此解決方案通常用於開發人員。您完全可以依賴Github中的依賴關係。我希望我的回答能解決你的問題。 我相信這是你在找什麼,結果loooks這樣的:
首先,你必須創建一個UIAlertController
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
自定義字體!即使只是某些角色。
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... StackOverFlow!"];
[hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30.0] range:NSMakeRange(24, 11)];
[alertVC setValue:hogan forKey:@"attributedTitle"];
現在,讓我們創建一個UIAlertAction,在這裏你可以添加動作處理程序,並添加圖標。
UIAlertAction *button = [UIAlertAction actionWithTitle:@"First Button"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
//add code to make something happen once tapped
}];
UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Second Button"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){
//add code to make something happen once tapped
}];
在這裏,您將圖標添加到AlertAction。對於我來說,你必須指定UIImageRenderingModeAlwaysOriginal
[button setValue:[[UIImage imageNamed:@"image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[alertVC addAction:button2];
[alertVC addAction:button];
記住要呈現的ViewController
[self presentViewController:alertVC animated:YES completion:nil];
![enter image description here](https://i.stack.imgur.com/l0E8f.png)
此方法不允許我調整按鈕高度。 UIAlert佔據了整個屏幕,這是非常侵入性的! – Olympiloutre
您的按鈕高度由您的圖標和字體的大小決定。否則它是默認大小。 – NeilNie
此解決方案利用未公開的私人功能,這些功能可能會在iOS的未來更新中崩潰(崩潰或無法正常工作)。最好避免這樣的代碼。 「UIAlertController」或「UIActionSheet」都不支持通過公共API提供的此功能。因此你應該找到一個定製的解決方案 – rmaddy