我在新的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.0let rect = CGRect(x: margin,height: 120)let customVIEw = UIVIEw(frame: rect)customVIEw.backgroundcolor = .greenalertController.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中看起来略有不同。
iOS 8
iOS 9
iOS 10
以上是内存溢出为你收集整理的swift – UIAlertController – 将自定义视图添加到 *** 作表全部内容,希望文章能够帮你解决swift – UIAlertController – 将自定义视图添加到 *** 作表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)