gpt4 book ai didi

ios - 为什么更改状态变量时界面会抽动?

转载 作者:行者123 更新时间:2023-11-28 23:23:53 24 4
gpt4 key购买 nike

内容 View :

@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”之间更改焦点时,它们开始抽搐,就像由于高度变化一样。我检查过,每次调用行初始值设定项时,但我不知道如何处理它。

最佳答案

发生这种情况是因为当前文本字段中的字体大小存在错误。如果您手动设置字体大小(例如 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)
}

关于ios - 为什么更改状态变量时界面会抽动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59191012/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com