使用我的尺码在 window 中打开模态 ViewController
Open modal ViewController in window with my sizes
如何使用我的尺码在 window 中打开模态 ViewController?
我试过了,没用
modalViewController?.view.superview?.frame = CGRect(x:162, y: 333, width: 700,height: 700)
//
modalViewController?.view.superview?.center = self.view.center
代码:
let modalViewController = self.storyboard?.instantiateViewController(withIdentifier: "Clipboard") as? ClipboardViewController
modalViewController?.modalPresentationStyle = .overCurrentContext
self.present(modalViewController!, animated: true, completion: nil)
示例:
尝试:
let modalViewController = self.storyboard?.instantiateViewController(withIdentifier: "Clipboard") as? ClipboardViewController
modalViewController?.modalPresentationStyle = .formSheet
self.present(modalViewController!, animated: true, completion: nil)
modalViewController!.preferredContentSize = CGSize(width: 700, height: 700)
由于您在 iPad 上,您可以通过以编程方式或在情节提要中设置视图控制器的首选内容大小来实现。
但我建议您使用 presentation controllers。
通过使用它们,您可以 return 您喜欢的尺寸或插图。
这是我的一个代码。
class PGSHelpPresentationController: UIPresentationController {
// The dimming view is the view the you have around the the content of the view controller you want to present
let dimmingView = UIView()
override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
// here the dimming view is white with an alpha of 0.5
dimmingView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
}
override public func presentationTransitionWillBegin() {
// Here I'm setting the initial properties of the dimming view (alpha and size)
dimmingView.frame = containerView!.bounds
dimmingView.alpha = 0.0
containerView!.insertSubview(dimmingView, at: 0)
//Here I'm asking to animate the change of alpha along with the transition animation of the view controller
presentedViewController.transitionCoordinator?.animate(alongsideTransition: {
context in
self.dimmingView.alpha = 1.0
},
completion: nil)
}
override public func dismissalTransitionWillBegin() { //Here I'm asking that once the dismissal begin I'd like also that the the dimming view refers to sully transparent
presentedViewController.transitionCoordinator?.animate(alongsideTransition: {
context in
self.dimmingView.alpha = 0.0
},
completion: {
context in
self.dimmingView.removeFromSuperview()
})
}
//Here I'm setting the size of the view controller that it should be displayed with an inset of 20pt
override public var frameOfPresentedViewInContainerView: CGRect {
return containerView!.bounds.insetBy(dx: 20, dy: 20)
}
override public func containerViewWillLayoutSubviews() {
dimmingView.frame = containerView!.bounds
presentedView!.frame = frameOfPresentedViewInContainerView
}
}
现在我可以创建自定义动画,这是一个简单的幻灯片过渡:
class PGSBouncyViewControllerAnimator: NSObject, UIViewControllerAnimatedTransitioning {
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.8
}
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
if let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to) {
let centre = presentedView.center
presentedView.center = CGPoint(x:centre.x,y: -presentedView.bounds.size.height)
transitionContext.containerView.addSubview(presentedView)
UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: { () -> Void in
presentedView.center = centre
}, completion: { _ in
transitionContext.completeTransition(true)
})
}
}
}
现在我创建了转换委托,该对象通过 return 演示控制器和自定义动画来管理转换。
public class PGSHelpTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return PGSHelpPresentationController(presentedViewController: presented,
presenting: presenting)
}
public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return PGSBouncyViewControllerAnimator()
}
}
如果您想呈现自定义视图控制器,只需在呈现视图控制器中将转换委托创建为 属性(lazy var bouncyTransitioningDelegate = PGSHelpTransitioningDelegate()
) 并执行此操作:
let busyViewController = BusyViewController()
busyViewController.transitioningDelegate = bouncyTransitioningDelegate
busyViewController.modalPresentationStyle = .custom
如何使用我的尺码在 window 中打开模态 ViewController?
我试过了,没用
modalViewController?.view.superview?.frame = CGRect(x:162, y: 333, width: 700,height: 700)
//
modalViewController?.view.superview?.center = self.view.center
代码:
let modalViewController = self.storyboard?.instantiateViewController(withIdentifier: "Clipboard") as? ClipboardViewController
modalViewController?.modalPresentationStyle = .overCurrentContext
self.present(modalViewController!, animated: true, completion: nil)
示例:
尝试:
let modalViewController = self.storyboard?.instantiateViewController(withIdentifier: "Clipboard") as? ClipboardViewController
modalViewController?.modalPresentationStyle = .formSheet
self.present(modalViewController!, animated: true, completion: nil)
modalViewController!.preferredContentSize = CGSize(width: 700, height: 700)
由于您在 iPad 上,您可以通过以编程方式或在情节提要中设置视图控制器的首选内容大小来实现。
但我建议您使用 presentation controllers。
通过使用它们,您可以 return 您喜欢的尺寸或插图。
这是我的一个代码。
class PGSHelpPresentationController: UIPresentationController {
// The dimming view is the view the you have around the the content of the view controller you want to present
let dimmingView = UIView()
override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
// here the dimming view is white with an alpha of 0.5
dimmingView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
}
override public func presentationTransitionWillBegin() {
// Here I'm setting the initial properties of the dimming view (alpha and size)
dimmingView.frame = containerView!.bounds
dimmingView.alpha = 0.0
containerView!.insertSubview(dimmingView, at: 0)
//Here I'm asking to animate the change of alpha along with the transition animation of the view controller
presentedViewController.transitionCoordinator?.animate(alongsideTransition: {
context in
self.dimmingView.alpha = 1.0
},
completion: nil)
}
override public func dismissalTransitionWillBegin() { //Here I'm asking that once the dismissal begin I'd like also that the the dimming view refers to sully transparent
presentedViewController.transitionCoordinator?.animate(alongsideTransition: {
context in
self.dimmingView.alpha = 0.0
},
completion: {
context in
self.dimmingView.removeFromSuperview()
})
}
//Here I'm setting the size of the view controller that it should be displayed with an inset of 20pt
override public var frameOfPresentedViewInContainerView: CGRect {
return containerView!.bounds.insetBy(dx: 20, dy: 20)
}
override public func containerViewWillLayoutSubviews() {
dimmingView.frame = containerView!.bounds
presentedView!.frame = frameOfPresentedViewInContainerView
}
}
现在我可以创建自定义动画,这是一个简单的幻灯片过渡:
class PGSBouncyViewControllerAnimator: NSObject, UIViewControllerAnimatedTransitioning {
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.8
}
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
if let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to) {
let centre = presentedView.center
presentedView.center = CGPoint(x:centre.x,y: -presentedView.bounds.size.height)
transitionContext.containerView.addSubview(presentedView)
UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: { () -> Void in
presentedView.center = centre
}, completion: { _ in
transitionContext.completeTransition(true)
})
}
}
}
现在我创建了转换委托,该对象通过 return 演示控制器和自定义动画来管理转换。
public class PGSHelpTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
public func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return PGSHelpPresentationController(presentedViewController: presented,
presenting: presenting)
}
public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return PGSBouncyViewControllerAnimator()
}
}
如果您想呈现自定义视图控制器,只需在呈现视图控制器中将转换委托创建为 属性(lazy var bouncyTransitioningDelegate = PGSHelpTransitioningDelegate()
) 并执行此操作:
let busyViewController = BusyViewController()
busyViewController.transitioningDelegate = bouncyTransitioningDelegate
busyViewController.modalPresentationStyle = .custom