Is there a way to add a label to a TextField in SwiftUI. I can't find anything in the documentation. I only see a way to add a placeholder.
TextField("Name", text: $name)
I'm looking to add a label to match the Picker styling
Picker(selection: $categoryId, label: Text("Category"))
I was able to get this to work by wrapping it in an HStack
HStack {
Text("Name")
Spacer()
TextField("", text: $name).multilineTextAlignment(.trailing)
}
Common idea is simple:
HStack {
Text("Name")
TextField("", text: $name)
}
everything's more is depending on your UI design needs.
Probably not the best solution, but if you give your Text views a fixed frame width and alignment, it will work. However you probably want to test this out with different accessibility settings.
HStack {
Text("Name").frame(width: 100, alignment: .leading)
TextField("Name", text: $name)
}
Related
.navigationTitle(Text("Hello").foregroundColor(.orange))
.navigationBarTitleDisplayMode(.inline)
How do I add a foreground color to a Navigation Bar Title in SwiftUI? This is a watchOS app and everything I tried doesn't work.
I have:
NavigationView {
// my content
}
.navigationBarTitle(Text("Hello"), displayMode: .inline)
.foregroundColor(.orange)
I found that this solution worked:
.navigationTitle{
Text("Hello")
.foregroundColor(.orange)
}
I have two TextEditors in a VStack in my app. I would like each of them to expand to fit the text they contain, but remain hugging the text. Unfortunately, long text in the first TextEditor expands both TextEditors equally. It is as if the total height of the VStack they are in is expanded correctly, but the extra height is shared between the two editors.
This only occurs when I use a maxWidth on the VStack. If I fix the width it behaves correctly.
Is there some way to solve this without losing the resize behaviour maxWidth gives?
ScrollView {
VStack(alignment: .leading) {
TextEditor(text: Binding($note.details)!)
.font(.body)
TextEditor(text: Binding($note.title)!)
.font(.title)
}.frame(maxWidth: 450)
}
Try to put .fixedSize(horizontal: false, vertical: true) on both editors.
I need to do textfield design like this. I tried many way but not found any solutions for it.
And in Google Material not yet for SwiftUI.
You may use it with .overlay and .storke:
TextField("Enter your email", text: $text)
.overlay(
RoundedRectangle(cornerRadius: 30)
.stroke(Color(.blue, lineWidth: 2))
Using iOS14.5, Swift5.4, XCode12.5, and SwiftUI,
I am trying to build simple DetailView to which you can navigate to using a NavigationLink.
It all works, except that I have no idea about the height of my NavigationBar inside the DetailView. (note that I use the .inline Version by calling .navigationBarTitleDisplayMode(.inline) on the DetailView).
I am trying to show a simple two-line VStack in the DetailView.
And it turns out that the DetailView's body is covered by the NavigationBar unless I move the whole thing down by 56 Pixels (by using Spacer().frame(height: 56)).
And I don't even know the precise height of the NavigationBar
And even worse, I don't know how to determine it dynamically for Portrait and Landscape rotations.
How do you make the DetailView's body start below the NavBar no matter rotation-states ?
Here example Screenshots for Portrait :
Left side: the title is cut off ! Right side: using a Spacer makes title appear
Here is how I open the navigationLink:
NavigationLink(
destination: DetailView(session: session).navigationBarTitleDisplayMode(.inline),
tag: session.id ?? "",
selection: $selectedTag,
label: { EmptyView() }
)
And here is the entire Detail-Screen:
(please notice the Spacer-distance of 56 in order to make the title visible at all)
import SwiftUI
struct DetailView: View {
#State var session: THSession
var body: some View {
VStack(alignment: .leading) {
Spacer().frame(height: 56) // !!!!!!!!!! without this the title sits behind NavBar !!!!!!!!!!!!!!!!!!!
Text(session.title)
.font(.title)
.fontWeight(.bold)
.foregroundColor(.white)
Spacer().frame(height: 16)
HStack {
Text(session.invitationCode)
.font(.title)
.fontWeight(.bold)
.foregroundColor(.white)
}
Spacer()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.background(Color.red)
.ignoresSafeArea()
}
}
I'm assuming that you're using ignoresSafeArea() because you want the background to extend into the safe area. But, where you have it placed is having a side effect of making the titles extend into the nav bar area.
Instead, place ignoresSafeArea() inside the background modifier:
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.background(Color.red.ignoresSafeArea())
Now, your titles won't extend into the safe area and you can avoid using a Spacer at the top of the View altogether.
I'm studying SwiftUI and when I using TextField then I have a problem with TextField's cursor is still at left-center of TextField's area.
How can I set it to left-top of TextField area.
Thanks!
This is my code:
TextField("What is your mind?", text: $statusContent)
.clipped()
.frame(height: 100, alignment: .leading)
.multilineTextAlignment(.leading)