如何在窗口中使用我的尺寸打开模态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)
示例:
答案 0 :(得分:3)
尝试:
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)
答案 1 :(得分:0)
由于您在iPad上,您可以通过以编程方式或在故事板中设置视图控制器的首选内容大小来实现。
但我建议你使用presentation controllers。
通过使用它们,您可以返回您喜欢的尺寸或插图
这是我的一个代码。
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)
})
}
}
}
现在我创建了转换委托,即通过返回表示控制器和自定义动画来管理转换的对象。
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