Ios 使用自定义模式演示时,如何更新PresentedViewContainerView的框架?
我正在使用自定义UIPresentationController以模式显示视图。显示视图后,显示视图中的第一个文本字段将成为第一个响应者,并显示键盘。为了确保视图仍然可见,我将其向上移动。但是,当我这样做时,PresentedViewContainerView的Ios 使用自定义模式演示时,如何更新PresentedViewContainerView的框架?,ios,swift,uipresentationcontroller,uimodalpresentationcustom,Ios,Swift,Uipresentationcontroller,Uimodalpresentationcustom,我正在使用自定义UIPresentationController以模式显示视图。显示视图后,显示视图中的第一个文本字段将成为第一个响应者,并显示键盘。为了确保视图仍然可见,我将其向上移动。但是,当我这样做时,PresentedViewContainerView的框架与视图的实际框架不再匹配。正因为如此,当我点击视图时,它被忽略了,因为在当前视图顶部的背景视图上有一个tapGestureRecogziner。如何通知presentingController presentedView的帧/位置已更
框架与视图的实际框架不再匹配。正因为如此,当我点击视图时,它被忽略了,因为在当前视图顶部的背景视图上有一个tapGestureRecogziner。如何通知presentingController presentedView的帧/位置已更改
在UIPresentationController中:
override var frameOfPresentedViewInContainerView: CGRect {
var frame = CGRect.zero
let safeAreaBottom = self.presentingViewController.view.safeAreaInsets.bottom
guard let height = presentedView?.frame.height else { return frame }
if let containerBounds = containerView?.bounds {
frame = CGRect(x: 0,
y: containerBounds.height - height - safeAreaBottom,
width: containerBounds.width,
height: height + safeAreaBottom)
}
return frame
}
override func presentationTransitionWillBegin() {
if let containerView = self.containerView, let coordinator = presentingViewController.transitionCoordinator {
containerView.addSubview(self.dimmedBackgroundView)
self.dimmedBackgroundView.backgroundColor = .black
self.dimmedBackgroundView.frame = containerView.bounds
self.dimmedBackgroundView.alpha = 0
coordinator.animate(alongsideTransition: { _ in
self.dimmedBackgroundView.alpha = 0.5
}, completion: nil)
}
}
以模式呈现视图:
let overlayVC = CreateEventViewController()
overlayVC.transitioningDelegate = self.transitioningDelegate
overlayVC.modalPresentationStyle = .custom
self.present(overlayVC, animated: true, completion: nil)
键盘出现时的动画(在显示的视图中):
@objc func animateWithKeyboard(通知:NSNotification){
让userInfo=notification.userInfo!
guard let keyboardHeight=(userInfo[UIResponder.keyboardFrameEndUserInfoKey]作为?NSValue)?.cgRectValue.height,
将duration=userInfo[UIResponder.keyboardAnimationDurationUserInfoKey]设为?双精度,
让curve=userInfo[UIResponder.KeyboardAnimationCurveUserInfo]作为?UInt else{
返回
}
//BottomControint是将内容固定到superview底部的约束。
让moveUp=(notification.name==UIResponder.keyboardWillShowNotification)
bottomConstraint.constant=moveUp?(键盘高度):原始底部值
让options=UIView.AnimationOptions(苹果文档中的rawValue:curve)
UIKit在测试过程中多次调用此方法
表示,因此您的实现应该返回相同的帧
矩形。不要使用此方法更改
查看层次结构或执行其他一次性任务
好的,如果您通过此变量指定帧,建议不要在整个演示过程中对其进行更改。如果您计划使用帧,请不要指定此变量,并在您的animator中手动处理Apple文档中的所有更改:
UIKit在测试过程中多次调用此方法
表示,因此您的实现应该返回相同的帧
矩形。不要使用此方法更改
查看层次结构或执行其他一次性任务
好的,如果您通过此变量指定帧,建议不要在整个演示过程中对其进行更改。如果您计划使用帧,请不要指定此变量,并在动画师中手动处理所有更改
@objc func animateWithKeyboard(notification: NSNotification) {
let userInfo = notification.userInfo!
guard let keyboardHeight = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height,
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt else {
return
}
// bottomContraint is the constraint that pins content to the bottom of the superview.
let moveUp = (notification.name == UIResponder.keyboardWillShowNotification)
bottomConstraint.constant = moveUp ? (keyboardHeight) : originalBottomValue
let options = UIView.AnimationOptions(rawValue: curve << 16)
UIView.animate(withDuration: duration, delay: 0,
options: options,
animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}