简体   繁体   English

模态视图未正确显示底视图

[英]Modal View not displaying bottom view correctly

I am currently presenting a modal with a view at the bottom.我目前正在展示一个底部有视图的模式。 Unfortunately, when the modal is displayed, the view controller is pushed down some and cuts off the bottom view found on this controller.不幸的是,当显示模态时,视图 controller 被向下推了一些,并切断了这个 controller 上的底部视图。 Is there a way to calculate how much the view is getting pushed down to move the bottom view a bit higher?有没有办法计算视图被推下多少以将底部视图移高一点? Thank you in advance!先感谢您!

I am presenting my modal through a navigation controller:我通过导航 controller 展示我的模式:

self.navigationController?.present(vc, animated: true, completion: nil)

On the modal presented view controller, view is added as follows:在模态呈现的视图controller上,添加视图如下:

    if let b = Bundle.main.loadNibNamed(String.init(format: "%@", "MyView"), owner: self, options: nil) {
        if let addedView = b[0] as? MyViewButton {
            addedView.configureOnScreen(view: self.View)
        }
    }

I am presenting my bottom view inside a custom class that extends UIView:我在扩展 UIView 的自定义 class 中展示我的底视图:

func configureOnScreen(view: UIView) {
let width = view.frame.width - self.frame.width
let height = view.frame.height - self.frame.height
self.frame = CGRect.init(x: width, y: height, width: self.frame.width, height: self.frame.height)
view.addSubview(self)
}

If you have your constraints setup correctly in your XIB, so they give it a width and a height, you can use auto-layout to position the loaded view.如果您在 XIB 中正确设置了约束,因此它们给它一个宽度和一个高度,您可以使用自动布局到 position 加载的视图。

Based on your code, it looks like you want the view at the bottom-right?根据您的代码,您似乎想要右下角的视图?

So, forget about your configureOnScreen() func in your MyViewButton class, and instead try it like this:所以,忘记MyViewButton class 中的configureOnScreen()函数,而是像这样尝试:

class PresentMeVC: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let b = Bundle.main.loadNibNamed(String.init(format: "%@", "MyView"), owner: self, options: nil) {
            if let addedView = b[0] as? MyViewButton {
                addedView.translatesAutoresizingMaskIntoConstraints = false
                view.addSubview(addedView)

                // respect the safe-area
                let g = view.safeAreaLayoutGuide
                NSLayoutConstraint.activate([
                    addedView.trailingAnchor.constraint(equalTo: g.trailingAnchor),
                    addedView.bottomAnchor.constraint(equalTo: g.bottomAnchor),
                ])
            }
        }
        
    }
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM