This should be a simple task. Following the example from the Apple Developers documentation (https://developer.apple.com/documentation/swiftui/scrollview), the data displayed doesn't scroll.
struct ContentView: View {
var body: some View {
ScrollView {
VStack(alignment: .leading) {
ForEach(0..<100) {
Text("Row \($0)")
}
}
}
}
}
I'm on Xcode 12.5.1. What am I missing here?
After much trial and error I finally figured out that each element needed to be focusable:
var body: some View {
ScrollView(.vertical) {
VStack(alignment: .leading) {
ForEach(0..<100) {
Text("Row \($0)")
.focusable()
}
}
}
}
If there is another solution for this I'd love to hear it.
Related
I use ZStack & NaviagtonLink in SwiftUI.
ZStack{
List{
NavigationLink(destination: NextView()) {
Text("Hi")
}
}
Text("Hello")
}
This view is valid as I expected, and there are the text "Hello" on List's columns.
But even in NavigationLink ZStack is valid and in NextView there is hello.
I want to display it only this view, and I don't want to display it in NextView,
Please tell me how to fix.
Thank you.
Embed in navigationView otherwise navigation link cannot work
struct ContentView: View {
var body: some View {
NavigationView{
ZStack{
List{
NavigationLink(destination: NextView()) {
Text("Hi")
}
}
Text("Hello")
}
.navigationBarHidden(true)
}
}
}
I'm pretty new to SwiftUI, trying to teach myself a few things here and there. But there's this one issue that's been eating at me for a while... and I can't figure out why the toolbar doesn't work/show for me.
The sample code is below, but the button doesn't show nor is there an actual bar. I have iOS 15.2, with XCode 13.2 beta.
TextField("placeholder", text: $text)
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
HStack {
Button(action: {
hideKeyboard()
}) {
Text("Done")
}
}
}
}
EDIT:
Figured out the reason why... it just wouldn't work in a scroll view for some reason. Anyone know why?
Under iOS 15.2 - Xcode 13.2 (not beta).
struct ContentView: View {
#State var text: String = ""
var body: some View {
NavigationView {
VStack {
Text(text)
TextField("Enter your name", text: $text)
}
.padding()
.navigationTitle("SwiftUI")
.toolbar {
// ToolbarItem(placement: .keyboard) {
// Button("Ok") {
// print("ok")
// }
// }
ToolbarItemGroup(placement: .keyboard) {
HStack {
Button("Press Me") {
print("Pressed")
}
Spacer()
Button(action: {
print("done")
}) { Text("Done") }
}
}
}
}
}
}
Make sure you put everything in a VStack or similar.
Xcode 13.3 (release version), in order for the toolbar to show up, the TextField and the .toolbar need to be inside of a NavigationView.
In my particular case I didn't want a NavigationBar, so I ended up with something like this to make it work:
var body: some View {
NavigationView {
VStack {
TextField("Enter your name", text: $text)
// Additional text fields go here, all text fields will get the toolbar.
}
.navigationTitle("")
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("Dismiss") {
print("Bismiss keyboard...")
}
}
}
}
}
if I place 2 or more Lists with items in VStack or HStack and give the Vstack .shadow attribute, only the first List is scrollable and receives "gesture" events. Anybody has an idea why and is this intended behaviour? Seems like a bug to me. Tried on Xcode and device with iOS 14 and 14.+
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
ListView1()
ListView2()
}
.shadow(radius: 5)
}
}
struct ListView1:View {
var values=["1","2","3"]
var body: some View {
List {
ForEach(values, id: \.self) { (value) in
Text("Value \(value)")
}
}
}
}
struct ListView2:View {
var values=["5","6","7"]
var body: some View {
List {
ForEach(values, id: \.self) { (value) in
Text("Value \(value)")
}
}
}
}
Use compositingGroup() for solving issue!
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
ListView1()
ListView2()
}
.compositingGroup() // <<: Here!
.shadow(radius: 5)
}
}
Wondering if anyone else has this issue, and if a workaround has been found. This works fine in iOS 13, but seems broken in iOS 14.
I am just trying to fire off a NavigationLink to another View, from a .contextMenu.
My code is as below.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
HStack {
Text("I am a text in a HStack ")
}
HStack {
NavigationLink(destination: TestView()) {
VStack {
Image(systemName:"gauge")
.font(.system(size: 31))
}
}
}
}
.contextMenu {
NavigationLink(destination: TestView()) {
Text("Navigate to TestView")
Image(systemName: "pencil")
}
}
}
}
}
The Destination TestView() is just a boilerplate "Hello World" view.
If I click not the Icon associated in the Stack, this triggers the navigation perfectly. But the same link in the context menu does not.
When I select it in the contextmenu, nothing happens. I.e I can select the menu item, but all it does is close the context menu and I stay on the same view.
Anyone else found this? solved it ?
Thanks
Here is a demo of possible approach. Tested with Xcode 12b3 / iOS 14 (also valid for SwiftUI 1.0)
struct ContentView: View {
#State private var showLink = false
var body: some View {
NavigationView {
VStack {
HStack {
Text("I am a text in a HStack ")
}
HStack {
NavigationLink(destination: Text("TestView")) {
VStack {
Image(systemName:"gauge")
.font(.system(size: 31))
}
}
}
}
.background(NavigationLink("", destination: Text("TestView"), isActive: $showLink))
.contextMenu {
Button(action: { self.showLink = true }) {
HStack {
Text("Navigate to TestView")
Image(systemName: "pencil")
}
}
}
}
}
}
I can't get a row to be selectable for something other than navigating, if there's also a NavigationLink in the row view. This is no problem with UIKit. Is it impossible because of lack of implementation at present, just a bug, or is it in fact presently possible?
This is probably the simplest snippet that can illustrate the problem.
struct ContentView: View {
var body: some View {
NavigationView {
List {
HStack {
Button("How can I tap over here without transitioning?") { }
NavigationLink( destination: ContentView() ) {
Spacer()
}
}
}
}
}
}
Your button should be not default style.
Here is a simple demo of possible approach. Prepared & tested with Xcode 11.7 / iOS 13.7
struct ContentView: View {
#State private var selected = false
var body: some View {
NavigationView {
List {
HStack {
Button(action: { self.selected.toggle() }) {
Image(systemName: selected ? "checkmark.circle" : "circle")
}.buttonStyle(PlainButtonStyle())
.padding(.horizontal)
NavigationLink( destination: Text("Details") ) {
Text("Link")
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
}
}
}