-1

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)
}
paul590
  • 1,385
  • 1
  • 22
  • 43
  • 2
    That looks like a very odd way to set the frame of a view. Most likely, you're doing that at the wrong point in the controller lifecycle. When / where are you executing that code? As a side note, you really should be using auto-layout constraints anyway. – DonMag Jan 04 '22 at 16:39
  • Thank you, I actually created an XIB to add this view manually into this viewController inside a storyboard. Would the constraints take into count the modal presentation? – paul590 Jan 04 '22 at 16:52
  • 1
    You need to show us how you have your XIB setup... how you're loading it... when that code is executing... etc. – DonMag Jan 04 '22 at 17:16
  • @DonMag thank you I edited my code – paul590 Jan 04 '22 at 17:24
  • 1
    A clear case of premature manual layout. See http://www.programmingios.net/premature-layout/ – matt Jan 04 '22 at 18:05
  • 1
    @paul590 ... you still haven't shown or explained how you have your XIB setup. Have you used constraints on the elements in that XIB so they define the width and height of the view? Or have you set a view size for your XIB, and then just positioned/sized its subviews without constraints? – DonMag Jan 04 '22 at 18:06
  • @matt thank you I will take a look into this, however, I do my loading on the ViewDidLoad would it still be considered premature? I am reading the article some more to make sure I didnt miss anything – paul590 Jan 04 '22 at 19:05
  • @DonMag I apologize, yes I have used constraints define the height and width of the view in the XIB file – paul590 Jan 04 '22 at 19:06
  • 1
    Yes, `viewDidLoad` is still premature. You do not yet know any actual heights / widths at that time. – matt Jan 04 '22 at 21:42

1 Answers1

1

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.

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Thank you for your response! I like this approach a lot better than doing the calculations I was doing to get it to the bottom right. The only issue I am still seeing is when I present the view as a modal, the button is getting cut off. Is there a constraint or anchor that provides us with how much of the view controller is getting cut off due to the way the modals are presented? – paul590 Jan 05 '22 at 14:47
  • 1
    @paul590 - do you mean the very bottom-right corner? Like this: https://i.stack.imgur.com/qtHDS.png ? If so, that's more of a *design* issue... the same thing happens if we put a button at very bottom-right on a non-presented controller. If your button is "more cut off" then it sounds like your constraints are not setup correctly in the XIB – DonMag Jan 05 '22 at 15:59
  • Thank you just to make sure, when presenting modally not full screen, the view, in general, gets pushed down some, which causes the bottom to get cut off. Technically, the constraints are working since it is anchored at the bottom position but the view is getting pushed down due to the modal presentation thats how I am perceiving this issue, is this not correct? when on modal view I can pull the view controller up a little more and see the rest of the button. – paul590 Jan 05 '22 at 16:48
  • 1
    @paul590 - no, that's not correct. See the **Edit** to my answer. If you're getting different results, you need to post more information (screen-shots, constraint setups, etc). – DonMag Jan 05 '22 at 17:30
  • thank you very much for this explanation! I will look at my code and make sure all constraints are in place. I will accept this answer since this explains it in great detail and gives me a great direction to move towards to. Thank you again for all your help! – paul590 Jan 06 '22 at 15:15