SwiftMessages icon indicating copy to clipboard operation
SwiftMessages copied to clipboard

Attach message only in top viewcontroller of a UINavigationController

Open giofid opened this issue 8 years ago • 13 comments

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

giofid avatar Mar 09 '17 16:03 giofid

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.

wtmoose avatar Mar 11 '17 23:03 wtmoose

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.

giofid avatar Mar 13 '17 12:03 giofid

In what way doesn't it work? I tested it using the demo app and it worked for me.

wtmoose avatar Mar 13 '17 13:03 wtmoose

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?

giofid avatar Mar 13 '17 13:03 giofid

Sounds like the message view is behind the nav bar. Try unchecking "Under Top Bars" in the storyboard for the explore view controller.

wtmoose avatar Mar 13 '17 15:03 wtmoose

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"?

giofid avatar Mar 13 '17 23:03 giofid

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:

SwiftMessagesTest.zip

wtmoose avatar Mar 14 '17 01:03 wtmoose

Thank you for your time, I thought it would be easier.

Unfortunately, in your attached example the message appears only the first time.

giofid avatar Mar 14 '17 08:03 giofid

I set the duration to .forever

wtmoose avatar Mar 14 '17 10:03 wtmoose

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?

giofid avatar Mar 14 '17 11:03 giofid

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.

wtmoose avatar Mar 14 '17 11:03 wtmoose

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.

giofid avatar Mar 14 '17 13:03 giofid

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.

wtmoose avatar Mar 14 '17 14:03 wtmoose