如何在Swift ios中创build自定义UIAlertController?
我正在试图使UIAlertController看起来像这样:
我们如何定制UIAlertController来获得与此图片相同的结果?
你想要做的是一个popup窗口,对于当前版本的iOS,你可以达到同样的效果,为iPad和iPhone。
1.-开始在Storyboard或xib上构build您的devise。 然后参考它。
2.-然后呈现为popover。
3.-也许你会想要实现popoverdeleates,以避免在旋转设备时出现错误的位置。
例如:
private static func presentCustomDialog(parent: UIViewController) -> Bool { /// Loads your custom from its xib or from Storyboard if let rateDialog = loadNibForRate() { rateDialog.modalPresentationStyle = UIModalPresentationStyle.Popover rateDialog.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve let x = parent.view.center let sourceRectX : CGFloat let maximumDim = max(UIScreen.mainScreen().bounds.height, UIScreen.mainScreen().bounds.width) if maximumDim == 1024 { //iPad sourceRectX = xx }else { sourceRectX = 0 } rateDialog.popoverPresentationController?.sourceView = parent.view rateDialog.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros rateDialog.popoverPresentationController?.sourceRect = CGRectMake(sourceRectX, xy, 0, 0) rateDialog.popoverPresentationController?.popoverLayoutMargins = UIEdgeInsetsMake(0, 0, 0, 0) rateDialog.popoverPresentationController?.delegate = parent rateDialogParent = parent dispatch_async(dispatch_get_main_queue(), { parent.presentViewController(rateDialog, animated: true, completion: nil) }) return true } return false }
更新:实现,点3 …在你的父母的
UIViewController
。
public class MyParentViewController: UIViewController, UIPopoverPresentationControllerDelegate { /** This function guarantees that the CustomDialog is always centered at parent, it locates the Dialog view */ public func popoverPresentationController(popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer<CGRect>, inView view: AutoreleasingUnsafeMutablePointer<UIView?>) { let x = popoverPresentationController.presentingViewController.view.center let newRect = CGRectMake(xx, xy, 0, 0) rect.initialize(newRect) } }
我做了@Hugo的自定义popup窗口,过了一段时间,我发现一个非常整洁和macros伟的方式,可以用来实现自定义popup窗口视图用很less的努力:
这里是Github上图书馆的链接:
https://github.com/m1entus/MZFormSheetPresentationController
它是用Objective C编写的,以及图书馆样本中包含的快速示例。
要把它包含到swift项目中,你需要使用一些名为Bridging-header的东西