加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:48:44 所属栏目:百科 来源:网络整理
导读:我正在尝试使UIAlertController看起来像这样: 我们如何定制UIAlertController以获得与此图片相同的结果? 你要做的是弹出窗口,对于当前版本的iOS,你可以为iPad和iPhone实现同样的效果. 1.-首先在Storyboard或xib上构建你的设计.然后引用它. 2.-然后把它作为
我正在尝试使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)
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读