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