我在 SwiftUI 中创建了一个文本字段和一个安全文本字段,但我不知道如何在 SwiftUI 中将图像添加到我的文本字段/安全文本字段中。SwiftUI 的在线文档并不像旧版本的 Swift 那样多。我还想将(占位符/输入文本)移动指定的数量,例如向右移动 30 点。我还试图查看背景颜色是否会从白色变为红色,但正如您所看到的,它在我的代码中对 UI 没有影响。
注意:我在代码中调用了 GeometryReader 以及用户名和密码的 @state 变量。
VStack (spacing: deviceSize.size.height * (50/812)) {
TextField ("Username", text: self.$username)
.foregroundColor(.black)//text color when you type
.accentColor(.blue)//cursor color
.background(Color(.red))//????
.textFieldStyle(RoundedBorderTextFieldStyle())
.cornerRadius(50)
// .border(Color.white)
//.font(.title)
SecureField ("Password", text: self.$password)
.textFieldStyle(RoundedBorderTextFieldStyle())
.cornerRadius(50)
}
.padding(.init(top: 0, leading: deviceSize.size.width * (38/375), bottom: 0, trailing: deviceSize.size.width * (38/375)))
Run Code Online (Sandbox Code Playgroud) 我只是在学习 Apple 的 SwiftUI,即使是做基础知识也有点令人沮丧。我试图将我的图像显示在屏幕的右上角,但默认情况下它显示在我的屏幕中央。任何人都知道我如何做到这一点。此外,我尝试根据屏幕尺寸调整图像大小,但我以前在常规 Swift 中使用的旧 self.frame.width 在 Swift UI 中不起作用。抱歉,这种在 SwiftUI 中编写代码的新方式对我来说很奇怪。
var body: some View {
ZStack {
//Define a screen color
LinearGradient (gradient: Gradient(colors:[Color(ColorsSaved.gitLabDark),Color(ColorsSaved.gitLabLight)]),startPoint: .leading,endPoint: .trailing)
//Extend the screen to all edges
.edgesIgnoringSafeArea(.all)
VStack(alignment: .trailing) {
Image("blobVectorDark")
.resizable()
.edgesIgnoringSafeArea(.top)
.frame(width: 60, height: 60)
// .offset(x: , y: 20)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试解决我的应用程序视图上有 2 个“后退按钮”的问题。这似乎是在我导航两次后发生的。一次从主屏幕创建帐户页面,然后再次从创建帐户页面转到另一个页面。使用导航视图
我将此格式用于不同的页面。
struct CreateAccountViewGeo: View {
var body: some View {
NavigationView{
GeometryReader { (deviceSize: GeometryProxy) in
ZStack{
NavigationLink(destination: SportPreference(), tag: 1, selection: self.$current) {
EmptyView()
}
VStack (spacing: deviceSize.size.height * (20/812)){
Button(action: {
self.current = 1
print("Create tapped!")
}) {
Text("Create")
.fontWeight(.bold)
.foregroundColor(.orange)
.frame(width: deviceSize.size.width*(185/375), height: deviceSize.size.height*(50/812))
.cornerRadius(50)
.overlay(
Capsule(style: .continuous)
.stroke(Color.orange, style: StrokeStyle(lineWidth: 2)))
.frame(width: deviceSize.size.width, alignment: .center)
}//Close button for create
}
}
}
}
Run Code Online (Sandbox Code Playgroud)