A Tyshka
A Tyshka

Reputation: 4100

Why is my view getting offset when changing presentation styles?

I have an iMessage extension and I'm having some issues with changing presentation styles. When I first open the app here is what I get:enter image description here That's how it should be. Now when I change to expanded presentation style, this is what I get:enter image description here That's also what I want. However, when I switch back to compact, this happens:enter image description here Here is my code:

override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
    super.didTransition(to: presentationStyle)
    presentSearchStickersView()
}
private func presentSearchStickersView() {
    let controller = (storyboard?.instantiateViewController(withIdentifier: "SearchStickersViewController"))! as! SearchStickersViewController
    controller.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
    controller.searchDelegate = self
    for child in childViewControllers {
        child.willMove(toParentViewController: nil)
        child.view.removeFromSuperview()
        child.removeFromParentViewController()
    }
    self.addChildViewController(controller)
    self.view.addSubview(controller.view)
}

And here is a screenshot of my top constraint:enter image description here

Upvotes: 1

Views: 247

Answers (2)

RomOne
RomOne

Reputation: 2105

In my point of view you should not reinstantiate the bar every time you switch to compact or extend mode. You should instantiate it once, then set constraints to the top of the view. I've tried that way and it's working fine ;)

So to sum up, if you are using the storyboard

  1. In your storyboard add your subview to the controller
  2. Set a Top constraint, width equal to superview and centerX to superview
  3. In the code set your search bar (delegate etc) in viewdidload

If you are not using storyboard.

  1. load your xib and add it to your subview (maybe in the didBecomeActive or something like that)
  2. Don't forget to set the translatesAutoresizingMaskIntoConstraints to false
  3. Add the same constraints as above

Upvotes: 1

Jason
Jason

Reputation: 826

As RomOne said you should be putting it there once. Style switches should be handled by constraints

Upvotes: 0

Related Questions