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

我正在尝试使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)
            rateDialog.popoverPresentationController?.popoverLayoutMargins = UIEdgeInsetsMake(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,0)
        rect.initialize(newRect)
    }

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...