swift – UIAlertController – 将自定义视图添加到操作表
发布时间:2020-12-14 05:56:54 所属栏目:百科 来源:网络整理
导读:当我们尝试附加一个图像在屏幕截图中时,我正在尝试制作在iOS上的消息应用程序中显示的操作。 我在新的UIAlertController中意识到,我们无法适应任何自定义视图。我可以做什么这样做吗? 我的代码看起来很标准 let alertController = UIAlertController(titl
当我们尝试附加一个图像在屏幕截图中时,我正在尝试制作在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: "nnnnnn",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: "nnnnnn",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:@"nnnnnn" 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中看起来略有不同。 iOS 8 iOS 9 iOS 10 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |