SwiftMessages
SwiftMessages copied to clipboard
Attach message only in top viewcontroller of a UINavigationController
Hi,
I'm evaluating to use your great library in my app. Only a question: in a UINavigationController based app is there a way to attach message only in top viewcontroller? In other words, when I pop away the viewcontroller, I'd like the message to disappear. I tried to play with presentationContext property but without success.
Thanks, Giorgio
If your view controller isn't set to extend under the top bar, you can use
config.presentationContext = .view(topViewController.view)
Otherwise, I don't have a solution for this yet.
I'm thinking it makes sense to change the behavior of the .viewController presentation context to display within the given view controller instead of the containing controller. But. I'll likely hold off on that until version 4 because it changes behavior.
Unfortunately your tip doesn't work neither with extend under the top bar enabled nor with extend under the top bar disabled.
I think some messages are meaningful only in the view controller where it's generated (a wrong user or password message in a login view controller, for example); so I'm agree with you to change the behavior of the .viewController presentation context mode.
In what way doesn't it work? I tested it using the demo app and it worked for me.
I also used the demo app. In ExploreViewController class I changed row 96 (Status Bar Level segment in Presentation Context mode) with
config.presentationContext = .view(self.navigationController!.topViewController!.view);
I launched app in iPhone7 simulator, in Presentation Context in the Explore view I selected Status Bar Level but nothing appear when I press Show button.
What am I doing wrong?
Sounds like the message view is behind the nav bar. Try unchecking "Under Top Bars" in the storyboard for the explore view controller.
I already try to uncheck "Under Top Bars" in the storyboard for the explore view controller but it doesn't work.
Please could you tell me how you made it work? What kind of "Presentation Context"?
Sorry, I wasn't thinking. This probably isn't ever going to work in a scroll view because the message would scroll too. I've never found a clean way to fix the on-screen position of a view within a scroll view.
But here is an example of my workaround in a non-scroll view:
Thank you for your time, I thought it would be easier.
Unfortunately, in your attached example the message appears only the first time.
I set the duration to .forever
Maybe, as workaround, might be easier showing the message in one of the mode already provided by "Presentation Context" and hiding it in viewWillDisappear method of the popping view controller.
Back to your example: if duration is .forever, why message disappears when go back in detail view controller?
As the documentation says, .forever means the message won't be automatically hidden by SwiftMessages. Naturally, when you go back, the view controller and its view hierarchy, including the message view, are destroyed.
.forever is an option I added to the sample project arbitrarily. You can remove it.
I didn't look into the library's code, but if the message view is destroyed and .forever is a property of the message view, I still can't figure out why when I go back in detail view and I press "Test" button nothing happens.
Oh, I get what you're saying now. I didn't think of that. If you use .forever, you're responsible for hiding it. I'll add it to my todo list to see if there's a way to make that automatic.