I am trying to put an option for .onlongpressgesture for this list of groups. However, it doesn't seem to work and I figure, it will work on Button.
I have tried to apply ".onTapGesture" and ".onLongPressGesture", but there is no effect.
Is there a way I can transform the Code below from a NavigationLink to a Button with the same destination when tapped and an additional Menu (called "OptionsmenuView") when long pressed?
The NavigationLink:
VStack (spacing: 20){
ForEach(groups, id:\.self) { Group in
NavigationLink(destination: GroupView()) {
ZStack (alignment: .bottomLeading) {
Image(uiImage: (UIImage(data: Group.groupThumbnail ?? self.image) ?? UIImage(named: "defaultGroupThumbnail"))!)
.resizable(capInsets: EdgeInsets())
.aspectRatio(contentMode: .fill)
.frame(height: 200.0, alignment: .center)
.cornerRadius(22)
VStack (alignment: .leading) {
Text("\(Group.groupTitle ?? "Untitled")")
.font(.title)
.fontWeight(.heavy)
.multilineTextAlignment(.leading)
Text("Consists of 5 Flowers")
}
.padding([.leading, .bottom], 18.0)
.foregroundColor(.primary)
}
.listRowBackground(Color.black)
}
}
}
The OptionMenuView:
struct OptionsMenuView: View {
var body: some View {
Menu {
Button("Cancel", role: .destructive) {
// Do something
}
Button {
// Do something
} label: {
Label("Edit", systemImage: "square.and.pencil")
}
Button {
// Do something
} label: {
Label("Delete", systemImage: "trash")
}
} label: {
Label("Settings", systemImage: "gearshape.fill")
}
}
}
I appreciate any form of advice. Thanks in advance.
Couldn't you just use .contextMenu ?
NavigationView {
List (0..<10) { i in
NavigationLink {
Text("DestionationView")
} label: {
Text("Item \(i)")
}
.contextMenu { // << here
Button("Cancel", role: .destructive) {
// Do something
}
Button {
// Do something
} label: {
Label("Edit", systemImage: "square.and.pencil")
}
Button {
// Do something
} label: {
Label("Delete", systemImage: "trash")
}
}
}
}
Related
I'm trying to figure out why the navigation title is not working with some devices, but I'm not able to figure out this issue. Can any one please help me to find out this issue, why the navigation title shows only the first 3 letters and after showing?
I have attached the Screenshot also please check.
iPhone 11 Device
iPhone 11 Simulator
Code:-
var body: some View {
ZStack {
Color.init(ColorConstantsName.MainThemeBgColour)
.ignoresSafeArea()
GeometryReader { geo in
ScrollView(.vertical) {
Text("Testing")
}
}
}
.navigationBarBackButtonHidden(true)
.navigationTitle(CommonAllString.BlankStr)
.navigationViewStyle(.stack)
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(leading:AnyView(leadingButton),trailing:AnyView(self.trailingButton))
.foregroundColor(Color.white)
}
var trailingButton: some View {
HStack {
Image(systemName: ImageConstantsNameForChatScreen.PersonImg)
.padding(.trailing)
Image(ImageConstantsName.DTRShareIconImg)
.resizable().frame(width: 20, height: 20)
}
}
Below Code Working:-
struct PSScreen: View {
var body: some View {
ZStack {
Color.init("Colour")
.ignoresSafeArea()
VStack{
Text("PSScreen")
}
}
.navigationBarBackButtonHidden(true)
.navigationBarTitle("", displayMode: .inline)
.navigationViewStyle(.stack)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
leadingButton.frame(width: 50, alignment: .leading)
}
ToolbarItem(placement: .navigationBarTrailing) {
trailingButton
}
}.foregroundColor(Color.white)
}
var leadingButton: some View {
HStack{
Text("Profile")
}
}
var trailingButton: some View {
HStack {
Image(systemName: "Person")
.padding(.trailing)
Image("Share")
.resizable().frame(width: 20, height: 20)
}
}
}
How can I influence the placement of a contextMenu in SwiftUI? I seem to be getting the exact opposite of what I want. What I want is the context menu to be aligned with the text as indicated in the arrows in the attached photos. The image contextMenus work perfect, but the text portion is where the challenge is. I have also tried changing the alignments, but no joy...
struct ContextMenuView: View {
var body: some View {
HStack {
ScrollView(.vertical, showsIndicators: false) {
HStack {
VStack() {
Text("Test").font(.footnote)
Text("Test").lineLimit(30)
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .trailing)
.padding()
.background(Color.blue)
.cornerRadius(10)
.contentShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
.contextMenu() {
Button {
print("Delete")
} label: {
Label("Delete this message", systemImage: "trash")
}
}
Image(systemName: "person.fill")
.contextMenu() {
Button {
print("Send")
} label: {
Label("Send private message", systemImage: "mail")
}
}
}.padding()
HStack {
Image(systemName: "person.fill")
.contextMenu() {
Button {
print("Send")
} label: {
Label("Send private message", systemImage: "mail")
}
}
VStack() {
Text("Test").font(.footnote)
Text("Test").lineLimit(30)
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.padding()
.background(Color.blue)
.cornerRadius(10)
.contentShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
.contextMenu() {
Button {
print("Delete")
} label: {
Label("Delete this message", systemImage: "trash")
}
}
}.padding()
}
}
}
}
I have a navigation title for a list view. After navigating back and forth, my navigation title is missing. I am new to swiftui and unable to debug. Kindly help.
var body: some View {
NavigationView {
VStack {
TabView(selection: $choice,
content: {
OPListCell()
IPListCell()
})
.tabViewStyle(PageTabViewStyle())
}
.listStyle(PlainListStyle())
.navigationBarTitle("My Patients")
.toolbar {
ToolbarItem(placement: .principal) {
HStack {
Picker(selection: self.$choice, label: Text("")) {
ForEach(0 ..< self.choices.count) {
Text(self.choices[$0])
}
}
.frame(width: 175)
.pickerStyle(SegmentedPickerStyle())
.padding(.leading, 10)
}
}
}
}
}
}
In Xcode 12 Beta 6, dismissing a sheet doesn't work inside a button's action inside a ToolbarItem.
My sheet view looks like:
NavigationView {
Form {
Section {
TextField("Name", text: $name)
}
}
.navigationTitle("New Thing")
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(action: {
self.presentation.wrappedValue.dismiss()
}, label: {
Text("Cancel")
})
}
ToolbarItem(placement: .confirmationAction) {
Button(action: {
do {
// some saving logic
try managedObjectContext.save()
self.presentation.wrappedValue.dismiss()
} catch {
print("didn't save due to \(error.localizedDescription)")
}
}, label: {
Text("Save")
})
}
}
}
EDIT: here's how I constructed the sheet
var body: some View {
List {
ForEach(results) { result in
HStack {
NavigationLink(destination: SingleResultView(result: result)) {
SingleResultRowView(result: result)
}
}
}
.onDelete(perform: deleteResult)
}
.navigationTitle("All Results")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button(action: {
self.isNewResultSheetPresented.toggle()
}, label: {
Image(systemName: "plus.circle.fill")
.resizable()
.frame(width: 30, height: 30, alignment: .center)
})
.sheet(isPresented: $isNewResultSheetPresented) {
NewResultView()
// ^ this contains the code above
.environment(\.managedObjectContext, self.managedObjectContext)
}
}
}
}
When the sheet is first presented, immediately a console log appears:
2020-09-13 20:52:02.333679-0700 MyApp[2710:89263]
[Presentation] Attempt to present <_TtGC7SwiftUI22SheetHostingControllerVS_7AnyView_: 0x1027b7890> on
<_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10270d620>
(from <_TtGC7SwiftUIP10$194f39bd428DestinationHostingControllerVS_7AnyView_: 0x103605930>)
which is already presenting <_TtGC7SwiftUI22SheetHostingControllerVS_7AnyView_: 0x103606d60>.
I can dismiss the sheet only by swiping down.
For reference, I went back to an older commit where I used NavigationBarItems and it worked perfectly. But from what I understand, this is a situation where I'm supposed to be using ToolbarItem.
Does anybody know why the good old self.presentation.wrappedValue.dismiss() doesn't work here or why is the sheet being presented twice?
Move sheet out of toolbar, as
var body: some View {
List {
ForEach(results) { result in
HStack {
NavigationLink(destination: SingleResultView(result: result)) {
SingleResultRowView(result: result)
}
}
}
.onDelete(perform: deleteResult)
}
.navigationTitle("All Results")
.sheet(isPresented: $isNewResultSheetPresented) { // << here !!
NewResultView()
// ^ this contains the code above
.environment(\.managedObjectContext, self.managedObjectContext)
}
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button(action: {
self.isNewResultSheetPresented.toggle()
}, label: {
Image(systemName: "plus.circle.fill")
.resizable()
.frame(width: 30, height: 30, alignment: .center)
})
}
}
}
I would like to show contextMenu by clicking on the left mouse button?
How to manually show view in SwiftUI?
Image(systemName: "book")
.contextMenu {
Text("something1")
Text("something2")
Text("something3")
}
You can use MenuButton with a menuButtonStyle to create a button which, when clicked, shows a menu. Seems to be Mac only currently.
MenuButton("Menu") {
Button(action: {
print("Clicked an item")
}) {
Text("Menu Item Text")
}
}.menuButtonStyle(BorderlessButtonMenuButtonStyle())
The currently accepted answer uses MenuButton, which is now deprecated. Below is how to use the new Menu option.
This is possible now via Menu (iOS 14+, MacOS 11+)
Menus are covered in this WWDC video at ~11:15.
Playground Example:
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
HStack {
// Other views
Text("Example View 1")
// Button, that when tapped shows 3 options
Menu {
Button(action: {
}) {
Label("Add", systemImage: "plus.circle")
}
Button(action: {
}) {
Label("Delete", systemImage: "minus.circle")
}
Button(action: {
}) {
Label("Edit", systemImage: "pencil.circle")
}
} label: {
Image(systemName: "ellipsis.circle")
}
}
.frame(width: 300, height: 300, alignment: .center)
}
}
PlaygroundPage.current.setLiveView(ContentView())
If you wish to support MacOS 10.15 you could do something like:
if #available(iOS 14, macOS 11, *) {
Menu {
Button(action: {
}) {
Label("Add", systemImage: "plus.circle")
}
Button(action: {
}) {
Label("Delete", systemImage: "minus.circle")
}
Button(action: {
}) {
Label("Edit", systemImage: "pencil.circle")
}
} label: {
Image(systemName: "ellipsis.circle")
}
} else if #available(macOS 10.15, *) {
MenuButton(
label: Image(nsImage: NSImage(named: NSImage.actionTemplateName)!),
content: {
Button(action: {}) {
HStack {
Image(nsImage: NSImage(named: NSImage.addTemplateName)!)
Text("Add")
}
}
Button(action: {}) {
HStack {
Image(nsImage: NSImage(named: NSImage.removeTemplateName)!)
Text("Delete")
}
}
Button(action: {}) {
HStack {
Image(nsImage: NSImage(named: NSImage.refreshTemplateName)!)
Text("Edit")
}
}
})
.menuButtonStyle(BorderlessButtonMenuButtonStyle())
} else {
// Custom code here would be needed to support iOS 13 and MacOS 10.14
}