View 在 iPhone plus 型号上显示不正确
View not presenting properly on iPhone plus models
在 iPhone 7 模拟器中,我有一个名为 cartView
的视图,看起来像这样...
按下底部 cartView
上的下一步按钮,会出现另一个视图,如下所示...
此呈现的视图称为 presentedView
。
按下 cartView
上的下一个按钮时编写的代码是这样的...
let vc = PresentedUserDetailsViewController()
vc.modalPresentationStyle = .custom
present(vc, animated: true, completion: nil)
在 presentedView
中,这些已在 viewDidLoad
...
之前声明
lazy var backdropView: UIView = {
let bdView = UIView(frame: self.view.bounds)
bdView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
return bdView
}()
let menuHeight = UIScreen.main.bounds.height / 2
var isPresenting = false
init() {
super.init(nibName: nil, bundle: nil)
modalPresentationStyle = .custom
transitioningDelegate = self
}
最后,在最后,也给了一个扩展名……
extension PresentedUserDetailsViewController: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 1
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
guard let toVC = toViewController else { return }
isPresenting = !isPresenting
if isPresenting == true {
containerView.addSubview(toVC.view)
menuView.frame.origin.y += menuHeight
backdropView.alpha = 0
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseOut], animations: {
self.menuView.frame.origin.y -= self.menuHeight
self.backdropView.alpha = 1
}, completion: { (finished) in
transitionContext.completeTransition(true)
})
} else {
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseOut], animations: {
self.menuView.frame.origin.y += self.menuHeight
self.backdropView.alpha = 0
}, completion: { (finished) in
transitionContext.completeTransition(true)
})
}
}
}
但是当我 运行 plus 类型的应用程序 iPhone 模型(比如 iPhone 6 plus 或 iPhone 7 plus)并按下下一步按钮时cartView
这就是我得到的...
在这里,我刚刚给 presentedView
上色以使其与众不同。在这种情况下,presentedView
不仅不会填满整个屏幕大小,而且 cartView
的一部分也会出现在后面,包括 cartView
的下一个按钮。
如何使 presentedView
正确显示在加号 iPhone 模型上,就像它们出现在 iPhone 7 模型中一样(从上数第二个屏幕截图)
编辑 1:iPhone 6 上的屏幕截图加上更改后..
编辑:2 如 iPad Air 2 中所示...
在这种情况下,您需要在添加子视图时设置约束。
containerView.addSubview(toVC.view)
使用自动布局来设置约束
**更新:**
extension UIView{
public func attachTo(view : UIView, animated : Bool = true){
if(animated){
self.alpha = 0
}
else{
self.alpha = 1
}
view.addSubview(self)
// self.frame = view.bounds
self.translatesAutoresizingMaskIntoConstraints = false
self.topAnchor.constraint(equalTo: self.superview!.topAnchor).isActive = true
self.bottomAnchor.constraint(equalTo: self.superview!.bottomAnchor).isActive = true
self.leadingAnchor.constraint(equalTo: self.superview!.leadingAnchor, constant: 0).isActive = true
self.trailingAnchor.constraint(equalTo: self.superview!.trailingAnchor, constant: 0).isActive = true
if(animated){
UIView.animate(withDuration: 0.3, animations: {
self.alpha = 1
})
}
}
}
现在,删除
containerView.addSubview(toVC.view)
并添加:
toVC.view.attachTo(view: containerView)
我想你可能在 let bdView = UIView(frame: self.view.bounds)
中弄错了框架。尝试使用自动布局
在 containerView.addSubview(toVC.view)
之后添加以下内容:
toVC.view.autoresizingMasks = [.flexibleWidth, .flexibleHeight]
在 iPhone 7 模拟器中,我有一个名为 cartView
的视图,看起来像这样...
按下底部 cartView
上的下一步按钮,会出现另一个视图,如下所示...
此呈现的视图称为 presentedView
。
按下 cartView
上的下一个按钮时编写的代码是这样的...
let vc = PresentedUserDetailsViewController()
vc.modalPresentationStyle = .custom
present(vc, animated: true, completion: nil)
在 presentedView
中,这些已在 viewDidLoad
...
lazy var backdropView: UIView = {
let bdView = UIView(frame: self.view.bounds)
bdView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
return bdView
}()
let menuHeight = UIScreen.main.bounds.height / 2
var isPresenting = false
init() {
super.init(nibName: nil, bundle: nil)
modalPresentationStyle = .custom
transitioningDelegate = self
}
最后,在最后,也给了一个扩展名……
extension PresentedUserDetailsViewController: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 1
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
guard let toVC = toViewController else { return }
isPresenting = !isPresenting
if isPresenting == true {
containerView.addSubview(toVC.view)
menuView.frame.origin.y += menuHeight
backdropView.alpha = 0
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseOut], animations: {
self.menuView.frame.origin.y -= self.menuHeight
self.backdropView.alpha = 1
}, completion: { (finished) in
transitionContext.completeTransition(true)
})
} else {
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseOut], animations: {
self.menuView.frame.origin.y += self.menuHeight
self.backdropView.alpha = 0
}, completion: { (finished) in
transitionContext.completeTransition(true)
})
}
}
}
但是当我 运行 plus 类型的应用程序 iPhone 模型(比如 iPhone 6 plus 或 iPhone 7 plus)并按下下一步按钮时cartView
这就是我得到的...
在这里,我刚刚给 presentedView
上色以使其与众不同。在这种情况下,presentedView
不仅不会填满整个屏幕大小,而且 cartView
的一部分也会出现在后面,包括 cartView
的下一个按钮。
如何使 presentedView
正确显示在加号 iPhone 模型上,就像它们出现在 iPhone 7 模型中一样(从上数第二个屏幕截图)
编辑 1:iPhone 6 上的屏幕截图加上更改后..
编辑:2 如 iPad Air 2 中所示...
在这种情况下,您需要在添加子视图时设置约束。
containerView.addSubview(toVC.view)
使用自动布局来设置约束
**更新:**
extension UIView{
public func attachTo(view : UIView, animated : Bool = true){
if(animated){
self.alpha = 0
}
else{
self.alpha = 1
}
view.addSubview(self)
// self.frame = view.bounds
self.translatesAutoresizingMaskIntoConstraints = false
self.topAnchor.constraint(equalTo: self.superview!.topAnchor).isActive = true
self.bottomAnchor.constraint(equalTo: self.superview!.bottomAnchor).isActive = true
self.leadingAnchor.constraint(equalTo: self.superview!.leadingAnchor, constant: 0).isActive = true
self.trailingAnchor.constraint(equalTo: self.superview!.trailingAnchor, constant: 0).isActive = true
if(animated){
UIView.animate(withDuration: 0.3, animations: {
self.alpha = 1
})
}
}
}
现在,删除
containerView.addSubview(toVC.view)
并添加:
toVC.view.attachTo(view: containerView)
我想你可能在 let bdView = UIView(frame: self.view.bounds)
中弄错了框架。尝试使用自动布局
在 containerView.addSubview(toVC.view)
之后添加以下内容:
toVC.view.autoresizingMasks = [.flexibleWidth, .flexibleHeight]