如何在 swift ios 中创建自定义 UIAlertController?

How to create Custom UIAlertController in swift ios?

我正在尝试制作如下所示的 UIAlertController:

我们如何自定义 UIAlertController 以获得与此图片相同的结果?

你想要做的是弹出窗口,对于当前版本的 iOS 你可以为 iPad 和 iPhone 实现相同的效果。

1.- 首先在 Storyboard 或 xib 上构建您的设计。然后引用它。

2.- 然后将其显示为弹出窗口。

3.- 也许您会想要实现 popoverdelegates 以避免旋转设备时位置错误。

例如:

  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 = x.x
            }else {
                sourceRectX = 0
            }

            rateDialog.popoverPresentationController?.sourceView = parent.view
            rateDialog.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros
            rateDialog.popoverPresentationController?.sourceRect = CGRectMake(sourceRectX, x.y, 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
    }

Update: to achieve, point 3... on your parent 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(x.x, x.y, 0, 0)
        rect.initialize(newRect)
    }

}

我做了自定义弹出窗口 windows 正如@Hugo 发布的那样,一段时间后我发现了一个库,它以非常简洁和宏伟的方式完成,可以用来更轻松地实现自定义弹出视图:

这是 Github 上图书馆的 link:

https://github.com/m1entus/MZFormSheetPresentationController

它是用 Objective c 编写的,并且库的示例中包含 swift 个示例。

要将其包含到 swift 项目中,您需要使用一种名为 Bridging-header 的东西