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

如何在swift ios中创建自定义UIAlertController?,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,我试图使UIAlertController看起来像这样: 我们如何定制UIAlertController以获得与此图片相同的结果?您尝试的是一个popover,对于当前版本的iOS,您可以在iPad和iPhone上实现相同的效果 1.-首先在故事板或xib上构建您的设计。然后引用它 2.-然后将其作为府绸展示 3.-可能您希望实现popoverdelegates,以避免旋转设备时出现错误位置 例如: private static func presentCustomDialog(paren

我试图使UIAlertController看起来像这样:


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

您尝试的是一个popover,对于当前版本的iOS,您可以在iPad和iPhone上实现相同的效果

1.-首先在故事板或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
    }
更新:要实现第3点。。。在您的家长
UIViewController

公共类MyParentViewController:UIViewController、UIPopoverPresentationControllerDelegate{
/**
此函数确保CustomDialog始终以父对象为中心,它定位对话框视图
*/
public func popoverPresentationController(popoverPresentationController:UIPopoverPresentationController,WillRespositionPopOverToRect:UnsafemeutablePointer,在视图中:AutoreleasingUnsafemeutablePointer){
设x=popoverPresentationController.presentingViewController.view.center
设newRect=CGRectMake(x.x,x.y,0,0)
初始化(newRect)
}
}

我在@Hugo post中创建了自定义弹出窗口,过了一段时间,我发现了一个库,它以一种非常简洁和华丽的方式完成,可以用它来实现自定义弹出视图,省力:

以下是Github上库的链接:

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


要将其包含到swift项目中,您需要使用一种称为桥接标头的工具,您需要使用该工具创建自己的弹出窗口design@JAL我所发现的只是UIAlertView,它已被弃用,我不知道从哪里开始this@EduardoIglesias你的意思是从一开始就实现UIAlertController中实现的一切吗?使用dim Effect,您无法自定义UIAlertController。苹果明确表示,不要将UIAlertController归类为子类。你必须制作自己的自定义控制器。好主意,我正在尝试如何制作这个全屏?可以全屏显示此弹出框吗?您可以通过将其原点(锚定)设置为(0,0),将其宽度和高度设置为
UIScreen.mainScreen().bounds
的宽度和高度…并删除为此弹出框设置的任何箭头来实现(未测试)。
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)
    }

}