簡體   English   中英

HStack 和 TextField 的角半徑未更新

[英]Corner Radius of both HStack and TextField not updating

這是我的內容視圖的代碼。 如您所見,我已經嘗試過使用 HStack 來包含 TextField,也嘗試過單獨使用 TextField。 角半徑與灰色搜索矩形沒有任何關系 - 邊緣仍然是完美的矩形。

    
    @State var searchText = ""
    var body: some View {
        ZStack {
            //function that's the content argument to ZStack
            Color((.systemGreen))
            VStack {
                Text("App")
                    .font(.largeTitle)
                    .foregroundColor(Color.white)
                //HStack {
                TextField("Searchstring", text: $searchText)
                    .padding()
                    .background(Color(.systemGray6))
                    .padding()
                    .cornerRadius(12)
                //}
//                .padding()
//                .background(Color(.systemGray6))
//                .padding(.horizontal)
//                .cornerRadius(12)
            }
        }
    }
}

這就是預覽的樣子,在所有情況下:角半徑無法在預覽中顯示

問題在這里:

.padding() /// 1.
.background(Color(.systemGray6)) /// 2.
.padding() /// 3.
.cornerRadius(12) /// 4.
  1. 您的文本字段有一個padding
  2. padding后應用background
  3. background之后添加另一個padding
  4. 您在padding頂部應用另一個cornerRadius

因此,圓角的是padding ,而不是background

顯示圓形透明填充的圖表

相反,您希望在background之后立即應用cornerRadius

顯示圓形背景的圖表,然后在外部填充

struct ContentView: View {
    @State var searchText = ""
    var body: some View {
        ZStack {
            //function that's the content argument to ZStack
            Color((.systemGreen))
            VStack {
                Text("App")
                    .font(.largeTitle)
                    .foregroundColor(Color.white)
                
                TextField("Searchstring", text: $searchText)
                    .padding()
                    .background(Color(.systemGray6))
                    .cornerRadius(12) /// corner radius immediately after the background
                    .padding() /// extra padding outside the background
            }
        }
    }
}

結果:

背景是圓形的,然后在外面應用填充

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM