内容视图:
@State var login: String = ""
@State var password: String = ""
@State var isLoginTextViewActive = false
@State var isPasswordTextViewActive = false
VStack {
TextField("Login", text: $login, onEditingChanged: { isEditing in
print(isEditing)
self.isLoginTextViewActive = isEditing
}
)
TextField("Password", text: $password)
HorizontalLine(color: isLoginTextViewActive ? Color.yellow : Color.black, height: CGFloat(1))
}.frame(width: CGFloat(200), alignment: .center)
行代码:
struct HorizontalLineShape: Shape {
func path(in rect: CGRect) -> Path {
let fill = CGRect(x: 0, y: 0, width: rect.size.width, height: rect.size.height)
var path = Path()
path.addRoundedRect(in: fill, cornerSize: CGSize(width: 2, height: 2))
return path
}
}
struct HorizontalLine: View {
private var color: Color? = nil
private var height: CGFloat = 1.0
init(color: Color, height: CGFloat = 1.0) {
self.color = color
self.height = height
}
var body: some View {
HorizontalLineShape().fill(self.color!).frame(minWidth: 0, maxWidth: .infinity, minHeight: height, maxHeight: height)
}
}
当您在“ TextView”之间切换焦点时,它们会由于高度的变化而开始抽动。每次调用线路初始化程序时,我都进行了检查,但是我不知道该怎么做。
答案 0 :(得分:1)
发生这种情况是因为当前文本字段中的字体大小存在错误。如果您手动设置字体大小(例如20),它将按需要工作。
var body: some View {
VStack {
TextField("Login", text: $login, onEditingChanged: { isEditing in
print(isEditing)
self.isLoginTextViewActive = isEditing
})
.font(.system(size: 20, weight: .medium, design: .default)) // SET SIZE
TextField("Password", text: $password)
.font(.system(size: 20, weight: .medium, design: .default)) // SET SIZE
Rectangle()
.frame(height: 1)
.foregroundColor(isLoginTextViewActive ? Color.yellow : Color.black)
}.frame(width: CGFloat(200), alignment: .center)
}