Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自己的警报控制器_Ios_Swift_Uiviewcontroller - Fatal编程技术网

Ios 自己的警报控制器

Ios 自己的警报控制器,ios,swift,uiviewcontroller,Ios,Swift,Uiviewcontroller,对于我的应用程序,我想实现我自己的AlertController。但我希望具有与UIAlertController相同的呈现行为:使用半透明背景使整个屏幕稍微暗一点,并且警报将以动画形式显示。我不希望它出现在模态演示中 理想情况下,我希望使用当前方法呈现我的AlertView,就像UIAlertView一样,例如: let alert = MyAlertController(type: .example) present(alert, animated: true, completion: ni

对于我的应用程序,我想实现我自己的AlertController。但我希望具有与UIAlertController相同的呈现行为:使用半透明背景使整个屏幕稍微暗一点,并且警报将以动画形式显示。我不希望它出现在模态演示中

理想情况下,我希望使用当前方法呈现我的AlertView,就像UIAlertView一样,例如:

let alert = MyAlertController(type: .example)
present(alert, animated: true, completion: nil)
编辑:对不起,在我看来,很明显我想要什么,但再次阅读问题,我发现了问题。。。这就是我到目前为止所尝试的。我创建了一个子类
UIViewController
,下面您可以看到(缩短的)代码

当前行为:当我呈现这个ViewController时,它只是以一种模态的方式呈现它,而不是变暗屏幕并弹出我的视图。我知道我需要实现动画,但在我想开始之前

期望的行为:我可以用与
UIAlertController
相同的方式使用我的类(见上文)。我只需要初始化控制器,然后调用present。我怎样才能做到这一点?我认为这一定是可能的,因为
UIAlertController
也使用了前面提到的方法。谢谢大家!

class FRAlertController: UIViewController {

private var alertView: UIView!


private var alertViewCenterXConstraint: NSLayoutConstraint!
private var alertViewCenterYConstraint: NSLayoutConstraint!


init(type: FRAlertControllerType, buttons: Bool = true) {
    super.init(nibName: nil, bundle: nil)

    setupView(type: type, buttons: buttons)
}

private func setupView(type: FRAlertControllerType, buttons: Bool) {        
    setupAlertView()
}

private func setupAlertView() {
    alertView = UIView()
    alertView.backgroundColor = .white
    view.addSubview(alertView)
    alertView.translatesAutoresizingMaskIntoConstraints = false

    alertViewCenterXConstraint = NSLayoutConstraint(item: alertView!, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0)
    alertViewCenterYConstraint = NSLayoutConstraint(item: alertView!, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)

    let widthConstraint = NSLayoutConstraint(item: alertView!, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width
        , multiplier: 0.7, constant: 0)
    let aspectRatio = NSLayoutConstraint(item: alertView!, attribute: .width, relatedBy: .equal, toItem: alertView, attribute: .height, multiplier: 1.05, constant: 0)

    view.addConstraints([
        alertViewCenterXConstraint,
        alertViewCenterYConstraint,
        widthConstraint,
        aspectRatio
    ])
}


override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
}
}

要使用自定义动画显示视图控制器,请执行以下操作:

  • 创建要显示的视图控制器
  • 创建自定义转换委托对象(
    UIViewControllerTransitioningDelegate
    ),并将其分配给视图控制器的
    transitioningDelegate
    属性。当被要求时,转换代理的方法应创建并返回自定义animator对象
  • 将视图控制器的
    modalPresentationStyle
    设置为
    .custom
    调用
    present:animated:
    方法来显示视图控制器

    参考资料:



    那么你的问题是什么?与其说我想做这个或那个,不如告诉我们你到目前为止到底做了什么。对不起,现在我更新了我的问题,它被重新打开了@Andreychernukhan现在我的问题更清楚了,我希望:)@iOSArchitect.com谢谢你,用一个相应的
    uiviewcontrollertransitiondelegate
    实现了
    UIViewControllerAnimatedTransitioning
    成功了!