缩放或更改hstack

问题描述 投票:0回答:0
而不是A

TextField

上,因为它可以让我更容易地拥有带有返回的多线。在Stack Overflow的各种文章之后(尤其是ThisOne

和ThisOne

)之后,我登上以下代码。
@State private var textEditorHeight : CGFloat = 100 private var maxHeight : CGFloat = 250 HStack { ZStack(alignment: .leading) { Text(newNoteText) .font(.system(.body)) .foregroundColor(.clear) .padding(10) .background(GeometryReader { Color.clear.preference(key: ViewHeightKey.self, value: $0.frame(in: .local).size.height) }) TextEditor(text: $newNoteText) .font(.system(.body)) .frame(height: min(textEditorHeight, maxHeight)) //.background(Color(NSColor.controlBackgroundColor)) .overlay(RoundedRectangle(cornerRadius: 5).stroke(.secondary)) .padding(.top, 5) .padding(.leading, 5) .padding(.bottom, 5) } .background(Color(NSColor.controlBackgroundColor)).edgesIgnoringSafeArea(.all) Button("Add") { guard !newNoteText.isEmpty else { return } viewModel.addNote(text: newNoteText) newNoteText = "" } .padding(.trailing, 10) } .onPreferenceChange(ViewHeightKey.self) { textEditorHeight = $0 } .background(Color(NSColor.controlBackgroundColor)).ignoresSafeArea().edgesIgnoringSafeArea(.all) struct ViewHeightKey: PreferenceKey { static var defaultValue: CGFloat { 0 } static func reduce(value: inout Value, nextValue: () -> Value) { value = value + nextValue() } }
这主要工作,但是HSTACK添加了按钮周围的区域和灰色的文本编辑器,而不是白色(在光模式下)。这样:

我能够通过添加
.background(Color(NSColor.controlBackgroundColor)).edgesIgnoringSafeArea(.all)

和不同的填充选项来删除其中的一些,但是顶部剩下的线:

enter image description here

搜索我找到的解决方案“

swiftui-如何更改视图的背景颜色?

我已经尝试了所有提出的解决方案。我还尝试了不同的padding()enter image description here选项和覆盖层,但似乎还没有奏效。 因此,我如何摆脱它,或更改其颜色以匹配文本编辑器的颜色?

谢谢你!

这里是我对我有用的测试代码。 此示例代码应隐藏按钮和文本编辑器周围的HSTACK

gray

区域。区域将采用背景颜色。
struct ContentView: View { var body: some View { ZStack(alignment: .leading) { // for testing, try green Color.white.ignoresSafeArea(edges: .all) TestView() } } } struct TestView: View { @State private var newNoteText = "" @State private var textEditorHeight: CGFloat = 100 let maxHeight: CGFloat = 250 var body: some View { HStack { ZStack(alignment: .leading) { Text(newNoteText) .font(.system(.body)) .foregroundColor(.clear) .padding(10) .background(GeometryReader { Color.clear.preference(key: ViewHeightKey.self, value: $0.frame(in: .local).size.height) }) TextEditor(text: $newNoteText) .font(.system(.body)) .frame(height: min(textEditorHeight, maxHeight)) .overlay(RoundedRectangle(cornerRadius: 5).stroke(.secondary)) .padding(.top, 5) .padding(.leading, 5) .padding(.bottom, 5) } // .background(Color.clear) // <--- here, if needed Button("Add") { guard !newNoteText.isEmpty else { return } // viewModel.addNote(text: newNoteText) newNoteText = "" } .padding(.trailing, 10) } .onPreferenceChange(ViewHeightKey.self) { textEditorHeight = $0 } .background(Color.clear) // <--- here, HStack color .edgesIgnoringSafeArea(.all) } } struct ViewHeightKey: PreferenceKey { static var defaultValue: CGFloat { 0 } static func reduce(value: inout Value, nextValue: () -> Value) { value = value + nextValue() } }

macos swiftui
© www.soinside.com 2019 - 2025. All rights reserved.