Search code examples
iosswiftswift5presentmodalviewcontroller

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. 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:

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

On the modal presented view controller, view is added as follows:

    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:

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)
}

Solution

  • 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.

    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:

    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),
                    ])
                }
            }
            
        }
        
    }
    

    Edit

    To try and clarify size / positioning...

    If we start with these two view controllers:

    enter image description here

    The blue button at bottom-right is the exact same size and position - constrained Trailing and Bottom to the safe-area.

    On launch, it looks like this (we're in a navigation controller):

    enter image description here

    The Top button presents the 2nd controller:

    enter image description here

    The Middle button presents the 2nd controller full-screen:

    enter image description here

    and the Bottom button pushes it onto the nav stack:

    enter image description here

    Note that the Height of the 2nd controller changes based on how it is displayed... the bottom of the controller is not being "pushed down."

    So, if your button is being cut-off more than shown here, then your constraints in your XIB are not being setup correctly.