Yas*_*ndo 7 objective-c uiimageview ios uialertcontroller
我想UIAlertController与提出警告UIImageView在ActionSheet.但是,当我运行应用程序时,它正在终止.
这是我的代码:
UIAlertController *alert = [UIAlertController
alertControllerWithTitle:@"Title"
message:@"Welcome"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okButton = [UIAlertAction
actionWithTitle:OK
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
}];
[alert addAction:okButton];
UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(20,20,50,50)];
imgv.image = [UIImage imageNamed:@"kaga.jpg"];
[alert setValue:imgv forKey:@"image"];
[self presentViewController:alert animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
您可以通过子类化添加图像在标题标签上方,UIAlertController并添加\n到标题字符串以为其创建空间UIImageView.您必须根据字体大小计算布局.对于UIAlertAction使用中的图像KVC如此:self.setValue(image, forKey: "image").我建议使用检查的扩展名responds(to:).
extension UIAlertAction {
/// Image to display left of the action title
var actionImage: UIImage? {
get {
if self.responds(to: Selector(Constants.imageKey)) {
return self.value(forKey: Constants.imageKey) as? UIImage
}
return nil
}
set {
if self.responds(to: Selector(Constants.imageKey)) {
self.setValue(newValue, forKey: Constants.imageKey)
}
}
}
private struct Constants {
static var imageKey = "image"
}
}
Run Code Online (Sandbox Code Playgroud)
UIAlertController根据Apple Doc,无法将图像添加到中。
如果需要,可以创建自己的自定义视图,例如:
https://github.com/wimagguc/ios-custom-alertview
如果您想拍摄图像,button
请尝试如下操作:
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Title"
message:@"Welcome"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* okButton = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
//Do some thing here
}];
[okButton setValue:[[UIImage imageNamed:@"kaga.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11753 次 |
| 最近记录: |