如何摆脱SwiftUI TextFields周围的蓝色选择边框?

时间:2020-01-15 15:50:17

标签: swift macos swiftui

我用以下代码创建了两个文本字段:


VStack (spacing: geometry.size.width/48) {
    TextField("World Name", text: self.$WorldName)
        .font(.system(size: geometry.size.width/28))
        .textFieldStyle(PlainTextFieldStyle())
        .frame(width: geometry.size.width*0.75)
        .background(
            RoundedRectangle(cornerRadius: 8)
                .fill(Color.init(white: 0.28))
    )
    TextField("World Seed", text: self.$WorldSeed)
        .font(.system(size: geometry.size.width/28))
        .textFieldStyle(PlainTextFieldStyle())
        .frame(width: geometry.size.width*0.75)
        .background(
            RoundedRectangle(cornerRadius: 8)
                .fill(Color.init(white: 0.28))
    )
    Button (action: {
        withAnimation {
            self.back.toggle()
        }
        // Is there a way to "deselect" any textfields here
    }){
        Text("Back")
    }
}

为什么单击它时会出现一个蓝色边框,该边框不会随动画一起淡出,我该如何删除它?这个问题是特定的,我提供了代码和必要的细节,我不明白为什么它太难回答了。

因此,概括地说,我需要知道:

  • 如何摆脱这个蓝色选择边框

  • 如何立即取消选择按钮操作中的文本字段,
  • 如果我应用填充或圆角,请获取边框以使其与TextField正确对齐。

此图片中唯一的蓝色是我指的边框 The only blue in this picture is the border I am referring to

如该屏幕快照所示,文本字段是圆形的,但是选择边框没有圆角以反映条目的圆角矩形形状 As shown in this screenshot, the textfield is round, but the selection border does not get round corners to reflect the rounded rectangle shape of the entry

蓝色边框不适合填充

The blue border does not fit the padding

我添加了一个.padding([.leading, .trailing], 6)这样的填充

2 个答案:

答案 0 :(得分:0)

我不知道您指的是哪个蓝色边框,如果您指的是textfield的蓝色边框,则没有蓝色边框,因为您给了PlainTextFieldStyle

要取消选择文本字段

UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.endEditing(true)

要使用填充填充四舍五入的textfield

ZStack {
    Rectangle()
    .foregroundColor(.clear)
    .overlay(RoundedRectangle(cornerRadius: 6).stroke(Color("appcolor").opacity(0.5), lineWidth: 1))

    TextField("Enter some text", text: $worldName)
        .padding([.leading, .trailing], 6)
}.frame(height: 42)
    .padding([.leading, .trailing], 10)

enter image description here

答案 1 :(得分:0)

您可以通过扩展NSTextField来删除蓝色边框(即使在使用IF OBJECT_ID('tempdb..#scale_table','U') IS NOT NULL DROP TABLE #scale_table; --SELECT * FROM #scale_table CREATE TABLE #scale_table ( empl_id int NOT NULL, scale_id varchar(100) NOT NULL, date_from date NOT NULL, date_to date NOT NULL ); INSERT INTO #scale_table (empl_id, scale_id, date_from, date_to) VALUES (187,'B3EL9', '2014-03-01','2017-06-30') , (187,'B4EL6', '2017-07-01','2019-10-31') , (187,'B5EL9', '2019-11-01','2099-12-31') , (214,'M115' , '2006-10-01','2099-12-31') , (618,'B3L9' , '2014-01-01','2019-10-31') , (618,'B6L9' , '2019-11-01','2099-12-31'); IF OBJECT_ID('tempdb..#value_table','U') IS NOT NULL DROP TABLE #value_table; --SELECT * FROM #value_table CREATE TABLE #value_table ( scale_id varchar(100) NOT NULL, amount decimal(10, 2) NOT NULL, date_from date NOT NULL, date_to date NOT NULL ); INSERT INTO #value_table (scale_id, amount, date_from, date_to) VALUES ('B3EL9',78084.00 ,'2013-01-01', '2015-06-30') , ('B3EL9',81432.00 ,'2015-07-01', '2099-12-31') , ('B4EL6',78348.00 ,'2013-01-01', '2015-06-30') , ('B4EL6',81720.00 ,'2015-07-01', '2099-12-31') , ('B5EL9',95964.00 ,'2013-01-01', '2015-06-30') , ('B5EL9',100092.00 ,'2015-07-01', '2099-12-31') , ('B3L9 ',52728.00 ,'2013-01-01', '2015-08-15') , ('B3L9 ',54996.00 ,'2015-08-16', '2017-11-30') , ('B3L9 ',56100.00 ,'2017-12-01', '2020-11-15') , ('B3L9 ',56664.00 ,'2020-11-16', '2099-12-31') , ('B6L9 ',64140.00 ,'2013-01-01', '2015-08-15') , ('B6L9 ',66900.00 ,'2015-08-16', '2017-11-30') , ('B6L9 ',68244.00 ,'2017-12-01', '2020-11-15') , ('B6L9 ',68928.00 ,'2020-11-16', '2099-12-31') , ('M115 ',108528.00 ,'2012-07-01', '2015-06-30') , ('M115 ',115128.00 ,'2015-07-01', '2099-12-31'); 时,它也会出现在macos上)

PlainTextFieldStyle

请参阅Apple开发者论坛答案here