I was wondering how to show a context menu when holding an element then showing a preview with the menu items in SwiftUI? I know how to show context menu items, but not with the preview and the animation.
View video: https://utilities.awesomeplayer.tech/send/f.php?h=35h_YJr5&p=1
I have tried to attach views inside the Context Menu, before the Buttons, but that has not worked.
Any help would be appreciated!
NOTE: The following is only available in iOS 16+ and iPadOS 16+. Earlier versions in second part. From the docs:
This view modifier produces a context menu on macOS, but that platform doesn’t display the preview.
Take a look at the docs here: https://developer.apple.com/documentation/swiftui/gridrow/contextmenu(menuitems:preview:). In iOS 16+, contextMenu accepts a preview argument that is some View. Example:
struct myViewWithPreview: View{
var body: some View{
Text("Hold for contextMenu")
.contextMenu{
//ContextMenu stuff here
//Such as buttons
Button("Example"){}
} preview: {//<-- HERE!!!!
//Here, put any view that you want as a preview. For example, you could put a webview here on a link.
//You could also do an image, such as how they do it on the docs.
Text("Preview!")
}
}
}
If you are using earlier OS versions, you must do this using a custom approach. Take a look at this article. This could also be of use: https://onmyway133.com/posts/how-to-show-context-menu-with-custom-preview-in-swiftui/.
Related
I am trying to display a two column NavigationView in my app exactly like the settings on the iPad. With no way to collapse the sidebar on an iPad. I would have thought using a NavigationView with DoubleColumnStyle would work but it doesn't and it's deprecated. I can use a NavigationSplitView like before to control the initial look however the user is still able to collapse the navigation sidebar.
I thought there would be a simple solution to this but have been looking for a while and haven't found any approach that works.
So far I have the following:
struct SettingsView: View {
#State private var columnVisibility = NavigationSplitViewVisibility.doubleColumn
var body: some View {
NavigationSplitView(columnVisibility: $columnVisibility) {
Text("Sidebar")
} detail: {
Text("Detail")
}
}
}
Here both the icon in the top left to hide the sidebar is generated automatically and also dragging the sidebar to the left closes it.
I have selectable Text, so when the user right clicks the text they see an edit menu with various options. I'd like to add some custom options to this menu but I'm not sure how to do this. If I can't add custom options, I'm also ok with completely replacing the menu altogether with my own custom menu.
Here is some sample code:
struct TestView: View {
var body: some View {
Text("Hello")
.textSelection(.enabled)
}
}
Here are some things I've tried:
adding a context menu with .contextMenu(). But right clicking will still only show the edit menu and not the context menu.
I've also tried disabling overriding right click behavior to stop the native edit menu from showing up (i.e. Override right click in SwiftUI), but that doesn't work.
Above mentioned solution is not working for my project in iOS , (this,"false") can you please help me to sort out the issue . Screenshot attached for your reference. How to hide back button in navigation bar xamarin forms
If you want to set the title of this page, set a SetBackButton on the page you want to return to.
You can add the following code to the page's constructor to hide the back button:
NavigationPage.SetHasBackButton(this, false);
As the title says, is there any way I can detect (e.g. using a #State variable) when either any context menu is open, or the context menu of a specific view is open?
As a basic idea, I would like to print something if it is open. This does not work:
.contextMenu {
print("open")
}
This also does not seem to work:
.contextMenu {
EmptyView()
.onAppear {
print("open")
}
}
How could i make this work?
Edit: Why i think its even possible to do it or at least possible to make it look like its possible: On instagram, one can see individual posts as a square only. However using long-press, a context menu opens, and now the post shape is different, but even more there is also a small title above it.. How would one do that? Did they modify the view when the context menu open or is the grid view the post is in before just hiding those details (true image shape + image title) but they are rendered already?
Screenshots:
A possible approach is to use simultaneous gesture for this purpose, like
Text("Demo Menu")
.contextMenu(menuItems: {
Button("Button") {}
})
.simultaneousGesture(LongPressGesture(minimumDuration: 0.5).onEnded { _ in
print("Opened")
})
Tested with Xcode 13.2 / iOS 15.2
I am trying to add an image next to the hamburger menu in my app. I am using a master detail page and it renders fine in ios and android. I tried using setting the icons for the detail pages. but the icons never showed. How do I achive this. I am looking to add an image similar to the amazon app.
I checked out this app on my phone and it uses a lot of custom constructions that aren't supported by out of the box Xamarin Forms controls. Something like the back arrow next to the hamburger menu is not a standard thing (on iOS at least). Also looking at the actual menu that the hamburger button triggers it becomes clear that its not an out of the box hamburger menu. Theirs pops over the content instead of sliding it out of view as the built-in one does.
More than likely they implemented their own hamburger menu and navigation structure which gave them the freedom to add buttons next to it. If you want to achieve this with out of the box controls you will most likely need custom renderers for each platform to create a replica of this navigation structure.
What is built-in in Xamarin Forms is that you can set the page title to an image instead of text by using the NavigationPage.SetTitleIcon method, which could be used to achieve what you want:
public class MyPage : NavigationPage
{
public MyPage ()
{
var myContentPage = new MyContentPage (Color.White);
this.Push (myContentPage);
var s = "icon.png";
NavigationPage.SetTitleIcon (myContentPage, s);
}
}
Even then, this is limited in functionality. If you want to fiddle with the alignment of the image e.g. you would also need to resort to a custom renderer.