After downloading Xcode 11 beta 5 i am seeing a lot of what appear to be 'random errors' (Please see code example below for more clarification) that i can not seem to resolve. I have read through the documentation and it's still a complete mystery to me.
struct RegistrationForm : View {
#Binding var emailAddress : String
#Binding var password : String
var body: some View{
VStack(alignment: .leading){ ///The error appears here **
Text(verbatim: "Email address")
.foregroundColor(Color.black)
.bold()
.font(.subheadline)
.padding(.leading, 12)
TextField($emailAddress)
.padding(.all)
Text(verbatim: "Password")
.foregroundColor(Color.black)
.bold()
.font(.subheadline)
.padding(.leading, 12)
SecureField($password)
.padding(.all)
}
}
}
///////Error message ///////
**
Static member 'leading' cannot be used on instance of type 'HorizontalAlignment'
As you can see from the code, i am applying .leading to a VStack, not a HStack.... Any ideas? As this error message is clearly incorrect.
You are using a no longer available initializer for TextField and SecureField:
Change your textfield to:
TextField("", text: $emailAddress)
And your secure textfield to:
SecureField("", text: $password)
Note: The first parameter is the placeholder
Related
I put "placeholders" in my TextEditor. I used .isEmpty but I soon learned that by typing in the TextEditor it gets rid of all placeholders. I only want it to get rid of the appropriate "placeholder" when the user types on that line but not get rid of them all at once. How might I achieve this?
I am starting to think .isEmpty is not the solution for this, but I'm not sure. Ideally I'd like to add a TextField INTO the TextEditor but I can't figure out how to do this.
Here is my .isEmpty code which gets rid of both "placeholders" when you type (or even press the space button):
ZStack (alignment: .leading){
if test.isEmpty{
VStack{
Text("Recipe Name")
.padding(.top,10)
.opacity(1)
.font(.system(size: 20))
.foregroundColor(.black)
Spacer()
}
}
if test2.isEmpty{
VStack{
Text("Recipe made by (link if applicable)")
.padding(.top, 50)
.opacity(1)
.font(.system(size: 20))
.foregroundColor(.black)
Spacer()
}
}
VStack{
TextEditor(text: $note.text)
.opacity(note.text.isEmpty ? 0.85 : 1)
.font(.custom("SanFrancisco", fixedSize: 20))
.onReceive(note.publisher(for: \.text), perform: setName)
.onReceive(
note.publisher(for: \.text)
.debounce(for: 0.5, scheduler: RunLoop.main)
.removeDuplicates()
){ _ in
try? PersistenceController.shared.saveContext()
}
.navigationTitle(note.name)
}
}
}
i want to put some fixed text inside TextField like "email: ....."
I tryed this:
HStack {
Image(systemName: "list.bullet").foregroundColor(.gray)
Text("email:")
TextField("", text: Binding(
get: { viewModel.email },
set: { viewModel.email = $0 }
))
.textFieldStyle(DefaultTextFieldStyle())
.frame(width: 60)
}
.padding()
.overlay(RoundedRectangle(cornerRadius: 10).stroke(
viewModel.emailValid() ? Color.gray : Color.red, lineWidth: 1
))
The problem is that this implementation only allows to click in specific part (inside the textField space) but i want to make all clickable to be more friendly to edit.
I tryed putting TextField and Text inside ZStack but the text entered by the user in inside te "Email text".
Maybe its possible to move the textField cursor to the end of the "email" text, or another implementation?
Thanks!
So if its mainly about activating the TextField on tap/click on any area of the element, you can use programmatic focus for TextField:
struct ContentView: View {
#State var input = ""
#FocusState var focused: Bool
var body: some View {
HStack {
Image(systemName: "list.bullet").foregroundColor(.gray)
Text("email:")
TextField("", text: $input)
.frame(width: 80)
.focused($focused)
}
.padding()
.overlay(RoundedRectangle(cornerRadius: 10).stroke(
true ? Color.gray : Color.red, lineWidth: 1
))
.contentShape(Rectangle()) // makes the whole element tappable
.onTapGesture {
focused = true // sets focus for textField
}
}
}
This is what my code is looking like:
import SwiftUI
struct dummy: View {
var job: Job
#ObservedObject var jobViewModel: JobViewModel
var body: some View {
Text("Placeholder for job details, will not exists in production, please delete")
VStack(alignment: .leading){
HStack{
Text("Boat Name")
Spacer()
.frame(width: 50, height: 50)
Text(job.boatName)
}
/*HStack{
Text("Job Time")
Spacer()
.frame(width: 50, height: 50)
Text(jobTime)
}*/
HStack{
Text("Job Type")
Spacer()
.frame(width: 50, height: 50)
Text(job.job)
}
HStack{
Text("Owner's Name")
Spacer()
.frame(width: 50, height: 50)
Text(job.ownerName)
//Text(job.time)
}
Button("Complete Job"){
jobViewModel.complete(job)
}.frame(width: 200)
}
}
}
/*struct dummy_Previews: PreviewProvider {
static var previews: some View {
dummy(job: Job[0])
}
}*/
//foundout to get rid of this from below:
//https://medium.com/swift-productions/create-list-navigation-using-swiftui-c63534355fb1
//If i didn't get rid of it, it would case issues
If I keep out the "PreviewProvider" it works, If i keep it in, it throws errors. I am building directly to a device every time I try the app. Do I need this so the ObservedObject works?
If you want to use the preview functionality, you'll have to provide some sort of instance for the jobViewModel parameter.
You haven't shown the code to JobViewModel, but it may be as simple as doing:
dummy(job: Job(), jobViewModel: JobViewModel())
This is assuming Job and JobViewModel don't take any parameters -- if they do, you'll have to fill in values for those parameters as well. Xcode should be able to help you with autocomplete to show you what their parameters may be.
Not having a preview will not affect your ability to run your app in non-preview mode on the simulator or a device.
I want to get text from a textfield.
My var is Entfernung:
#State var entfernung = ""
And here is my HStack where the TextField is in:
HStack{
Text(LocalizedStringKey("entfernung")).font(.title).bold().padding(.leading, 15).padding(.top, 30)
TextField("Username", text: $entfernung)
.font(.system(size: 20)).padding(.horizontal).background(Color.green)
Text(entfernung)
Text(LocalizedStringKey("entfernung")).font(.title).bold().padding(.leading, 15).padding(.top, 30)
}
But I get the error that I have to put self. infront of $entfernung. But when I change the code I am not able to edit the Textfield in my program.
HStack{
Text(LocalizedStringKey("entfernung")).font(.title).bold().padding(.leading, 15).padding(.top, 30)
TextField("Username", text: self.$entfernung)
.font(.system(size: 20)).padding(.horizontal).background(Color.green)
Text(self.entfernung)
Text(LocalizedStringKey("entfernung")).font(.title).bold().padding(.leading, 15).padding(.top, 30)
}
Does anyone know what my problem is?
I have a simple log in view comprising of two text fields, a navigation link to a registration view, a navigation link to a forgot password view and a log in button. I can select either text field and enter text, however attempting to press any other control on the view (textfield or navigation link) after entering text into either text field, simply freezes the app with no error messages logged in the console window.
I have seen in other posts relating to older versions of Xcode / swift that this type of behaviour can be explained by a continuous loop being executed on the main thread, however i can not see how this applies in my case?
struct LoginForm : View {
#Binding var emailAddress : String
#Binding var password: String
var body: some View{
VStack(alignment: .leading){
Text("Email address")
.foregroundColor(Color.black)
.bold()
.font(.subheadline)
.padding(.leading, 12)
TextField("Email",text:$emailAddress)
.padding()
Text("Password")
.foregroundColor(Color.black)
.bold()
.font(.subheadline)
.padding(.leading, 12)
SecureField("Password",text:$password)
.padding()
}
}
}
struct LoginRootView : View {
#State private var emailAddress: String = ""
#State private var password: String = ""
#EnvironmentObject var authData: AuthData
var body: some View {
Group{
if self.authData.authenticationDidSucceed {
OnBoardRootView()
}else{
NavigationView{
ZStack{
Image("LoginBG")
.resizable()
.aspectRatio( contentMode: .fill)
.edgesIgnoringSafeArea(.all)
.opacity(0.5)
VStack(alignment: .leading){
Text( "Log into")
.font(.largeTitle)
.bold()
Text( "your account")
.font(.largeTitle)
.bold()
LoginForm(emailAddress:$emailAddress, password:$password)
.padding(.bottom, 8)
.padding(.top, 22)
//Login button
if emailAddress != "" && password != ""{
LoginButton(email:self.emailAddress, password: self.password)
.padding(.vertical, 20.0)
.background(Color.blue, cornerRadius: 30.0)
.padding(.horizontal, 8)
}else{
LoginButton(email:self.emailAddress, password: self.password)
.padding(.vertical, 20.0)
.background(Color.gray, cornerRadius: 30.0)
.padding(.horizontal, 8)
.disabled(emailAddress == "" || password == "")
}
//Forgot password link
HStack{
Text( "Forgot password?")
NavigationLink(destination: ForgotPasswordRootView()) {
Text( "Get help signing in")
.bold()
}
}.padding(.top, 20)
Spacer()
// Registration Link
HStack{
Text( "Don't have an account? ")
NavigationLink(destination: RegistrationRootView()) {
Text( "Sign Up")
.bold()
}
}
.padding(.bottom, 120)
}.padding(.all)
.padding(.top, 160)
// end of vstack
}
//end of zstack
}
//end of navigation view
}
//end of authentication if statement
}
//end of group
}
}
You have to reset the simulator.
Simulator:
Hardware -> Erase All Content And Settings
Xcode:
Make clean build
Run
Just to Restart the simulator can solve the issue.
Simulator -> Hardware -> Restart