当我们尝试附加一个图像在屏幕截图中时,我正在尝试制作在iOS上的消息应用程序中显示的操作。
我在新的UIAlertController中意识到,我们无法适应任何自定义视图。我可以做什么这样做吗?
我的代码看起来很标准
let alertController = UIAlertController(title: "My AlertController",message: "tryna show some images here man",preferredStyle: UIAlertControllerStyle.ActionSheet) let okAction = UIAlertAction(title: "oks",style: .Default) { (action: UIAlertAction) -> Void in alertController.dismissViewControllerAnimated(true,completion: nil) } let cancelAction = UIAlertAction(title: "Screw it!",style: .Cancel) { (action: UIAlertAction) -> Void in alertController.dismissViewControllerAnimated(true,completion: nil) } alertController.addAction(okAction) alertController.addAction(cancelAction) self.presentViewController(alertController,animated: true,completion: nil)
UIAlertController扩展了UIViewController,它具有一个view属性。您可以将该视图的子视图添加到您心中的愿望。唯一的麻烦是正确地调整警报控制器的大小。你可以做这样的事情,但是这可能会在下次苹果调整UIAlertController的设计时轻松破解。
Swift 3
let alertController = UIAlertController(title: "\n\n\n\n\n\n",message: nil,preferredStyle: UIAlertControllerStyle.actionSheet) let margin:CGFloat = 10.0 let rect = CGRect(x: margin,y: margin,width: alertController.view.bounds.size.width - margin * 4.0,height: 120) let customView = UIView(frame: rect) customView.backgroundColor = .green alertController.view.addSubview(customView) let somethingAction = UIAlertAction(title: "Something",style: .default,handler: {(alert: UIAlertAction!) in print("something")}) let cancelAction = UIAlertAction(title: "Cancel",style: .cancel,handler: {(alert: UIAlertAction!) in print("cancel")}) alertController.addAction(somethingAction) alertController.addAction(cancelAction) DispatchQueue.main.async { self.present(alertController,completion:{}) }
迅速
let alertController = UIAlertController(title: "\n\n\n\n\n\n",preferredStyle: UIAlertControllerStyle.actionSheet) let margin:CGFloat = 10.0 let rect = CGRect(x: margin,height: 120) let customView = UIView(frame: rect) customView.backgroundColor = .green alertController.view.addSubview(customView) let somethingAction = UIAlertAction(title: "Something",handler: {(alert: UIAlertAction!) in print("something")}) let cancelAction = UIAlertAction(title: "Cancel",handler: {(alert: UIAlertAction!) in print("cancel")}) alertController.addAction(somethingAction) alertController.addAction(cancelAction) self.present(alertController,completion:{})
Objective-C的
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; CGFloat margin = 8.0F; UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(margin,margin,alertController.view.bounds.size.width - margin * 4.0F,100.0F)]; customView.backgroundColor = [UIColor greenColor]; [alertController.view addSubview:customView]; UIAlertAction *somethingAction = [UIAlertAction actionWithTitle:@"Something" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}]; [alertController addAction:somethingAction]; [alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:^{}];
话虽如此,一个不那么诡异的方法就是使你自己的视图子类与UIAlertController的UIAlertActionStyle布局类似。事实上,相同的代码在iOS 8和iOS 9中看起来略有不同。