Setting the focus/firstResponder on a control improves the user experience and is in my eyes absolutely essential. I expected that SwiftUI will support this from now on. Very disappointing, that it doesn't do it. But there is some time until the final release and I hope that Apple will listen to our wishes. I would be very happy if SwiftUI would support it. Would be nice to do something like this:
struct ContentView: View {
@Environment(\.firstResponder) private var firstResponder: ObjectIdentifier
@State var text1: String = ""
@State var text2: String = ""
var body: some View {
VStack {
TextField("...", text: self.$text1)
.id("TF1")
TextField("...", text: self.$text2)
.id("TF2")
HStack {
Button("Focus 1") {
self.firstResponder = "TF1"
}
Button("Focus 2") {
self.firstResponder = "TF2"
}
}
}
}
}
Post
Replies
Boosts
Views
Activity
You can:
struct ContentView: View {
		@State var text: String = ""
var body: some View {
		TextEditor(text: self.$text)
		.multilineTextAlignment(.center)
}
}
Last year I started a bigger project with Catalyst, because I didn't want to write code twice (as some controls didn't exist at this time, so I had to rely on UIKit... therefore I didn't want to write code with UIKit and AppKit to support both). This year now there are some more controls, but still you cannot build an app 100 % with SwiftUI, unfortunately. I'm still thinking about how I will do this now, if I should continue using Catalyst or switch to the Multiplatform template.
Are there any official longterm plans? I think if Apple is now pushing the iOS frameworks to macOS and if now the design of macOS is getting closer to iOS/iPadOS, there will be hopefully in the end no need for Catalyst anymore?
@mohamedfromparis: I'm sorry, I read it too fast! I don't know, what you want to achieve exactly. What will be before the text? Line breaks? If not, what will happen if you type in your text and then create a new line? Will your TextEditor resize?
What kind of user input? Please explain a little bit more, what you want to do.
This has nothing to do with SwiftUI. SwiftUI renders your controls in your macOS or iOS-application. You need to think about how you present your values in a pdf. You need to create the pdf in your Swift code. For this you can choose different ways, for example you can choose a framework for that (i. e. PDFKit from Apple) or you create an HTML-file and render this to a pdf. You find a lot of stuff if you google for "Swift create PDF"
I have the same problem with swift-nio and swift-nio-ssl. For me this way doesn't work, I tried several combinations... I think a problem could be, that for example CNIOBoringSSL is not a Swift package, it doesn't have a Package.swift. Any advice how to solve this problems when using swift-nio and swift-nio-ssl?
@Jim Dovey: Thank for that good explanation. I had this problem as well more then once and I also found out, that SwiftUI completely rerenders the whole UI altough only one property is changed. The example of tom.king can be easily splitted up into @State so you can avoid this problem.
But how to do with sth. more complex like this?
class ViewModel: ObservableObject {
		@Published var documents: [Document]
		struct Document {
				var fileName: String = ""
				var documentName: String = ""
				var someOtherInfo = 4
		}
		init(fieldCount: Int) {
				self.documents = Array<Document>.init(repeating: Document(), count: fieldCount)
		}
}
struct ContentView: View {
		@ObservedObject var viewModel = ViewModel(fieldCount: 20)
		var body: some View {
				ScrollView(.vertical) {
						VStack(alignment: .leading) {
								ForEach(self.viewModel.documents.indices) { index in
										DocumentView(viewModel: self.viewModel, index: index)
								}
						}
				}
		}
}
struct DocumentView: View {
		@ObservedObject var viewModel: ViewModel
		var index: Int
		var body: some View {
				VStack(alignment: .leading) {
						Text("\(index)")
						HStack {
								Text("Filename:")
										.frame(width: 200)
								TextField("", text: self.$viewModel.documents[index].fileName)
										.textFieldStyle(RoundedBorderTextFieldStyle())
						}
						HStack {
								Text("DocumentName:")
										.frame(width: 200)
								TextField("", text: self.$viewModel.documents[index].documentName)
										.textFieldStyle(RoundedBorderTextFieldStyle())
						}
				}
				.padding()
				.background(Rectangle().fill(Color(#colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1))).cornerRadius(15))
		}
}
@den73: Nice solution and it seems to work, at least partly. Do you also experience the issue, that sometimes on the first few clicks the element is not selected correctly with the blue selection? For me it happens, that there is a grey selection.
EDIT
Your solution also seems to break the behaviour, that you can move the selection while click and hold, then move the mouse over the items.
Unbelievable, that there is still no solution from Apple.
Thank you very much for your detailed answer!
Your're right, I just tested it with a simple .txt-file. lsof only checks, if there is an open file descriptor, but of course, there is none, if the file is completely read to the memory and then closed. It was true for pdf files opened by Preview (I just tested them so far).
Wow, that completely changes my plans, because it then seems, that there is no possibility to know, if my file is opened by another application or not. In the case of a txt-file it even would lead to the opposite, as then I would get notified, that the file was opened and then immediately closed.
Anyways, I could maybe implement it for those files (like pdf or other files), where the application keeps the file descriptor opened. So independently to my use case, is there any API, with which I get notified, when the file (desriptor) is opened or closed?
EDIT: After reading your linked topic, it seems to be only possible with Endpoint Security system extension?
Thank you very much. This really did bring a lot of light into my question.
I took a look on NSFilePresenter and at least I now can easily find out, that the file is changed and it seems to work even if the target application works with a file copy.
But yes, the main problem still will be, that apps can just load the file into memory and then close the file descriptor. Then there is no chance to know, if the file is still opened, except that I know, in which way the application creates those file copys for safe-save.
I have exactly the same issue. The "_hitTestForEvent" method only takes 38,3 %, but the "_routeMouseMovedEvent" has 55,8 % (I am using the Magic Mouse instead of the touch pad).
For me it seems to be a bug in SwiftUI.
@ugglaguggla : Do you experience the same behaviour in the Xcode Preview? I only have the issue when using the compiled version.
I just filed a bug (FB15269193). It happens only in very long lists and only when scrolling down the first time. List seems to recalculate the heights of the cells and there must be a bug. I hope, that Apple will fix this in the next release.
Thank you very much for your answer. Did you saw the video I made from the behaviour? It's interesting, that you couldn't reproduce. Please scroll nearly to the bottom and then select an item.
In my other project I could solve the problem after deleting everything what was displayed in the list and I only had one Text element. But that surely cannot be the solution ;)