4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SwiftUIで TextField にクリアボタンをつける方法

Posted at

TextField にクリアボタンを付けたい

 TextField に文字が入力されたら、フイールドの右端に入力されたテキストを全部消すためのクリアボタンを付けたいと思ってネットを探したが、結構古い情報があって、やってみるとエラーになるものが多かった。
かつては UITextField のプロパティ ClearButton の設定を「Is always visible」に設定するだけだったのだが、SwiftUIのフレームワークにある TextField には ClearButton プロパティが無いので、自分で実装する必要があるらしい。

 成功した方法を見つけたサイトの情報は末尾に書いておくので詳しくはそちらを。

実現できたソース

クリアボタンを付けることができた。 
クリアボタンはテキストフィールド内に表示させる場合と、テキストフィールドの外に表示させる場合の2種類を紹介する。

(1)テキストフィールド内にクリアボタンを付ける

Simulator Screen Shot - iPod touch (7th generation) - 2022-01-26 at 09.16.34.png
 テキストフィールド内にクリアボタンを付けるソースは以下のとおり。

ContentView.swift
import SwiftUI

struct ContentView: View {
    @State var text: String = ""
    var body: some View {
        Form {
            Section() {
                TextField("NAME", text: $text)
                    .modifier(TextFieldClearButton(text: $text))
                    .textFieldStyle(RoundedBorderTextFieldStyle())
            }
        }
    }
}

struct TextFieldClearButton: ViewModifier {
    @Binding var text: String
    
    func body(content: Content) -> some View {
        ZStack(alignment: .trailing)
        {
            content
            if !text.isEmpty {
                Button(
                    action: {
                        self.text = ""
                        
                    })
                {
                    Image(systemName: "delete.left")
                        .foregroundColor(Color(UIColor.opaqueSeparator))
                }
                .padding(.trailing, 8)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

テキストフィールドの右外にクリアボタンを付ける

Simulator Screen Shot - iPod touch (7th generation) - 2022-01-26 at 09.30.33.png

  テキストフィールドの外側(右側)にクリアボタンを付けるソースは以下のとおり。

ContentoView.swift
import SwiftUI

struct ContentView: View {
    @State var text: String = ""
    var body: some View {
        Form {
            Section() {
                TextField("NAME", text: $text)
                    .modifier(TextFieldClearButton(text: $text))
                    .textFieldStyle(RoundedBorderTextFieldStyle())
            }
        }
    }
}

struct TextFieldClearButton: ViewModifier {
    @Binding var text: String
    
    func body(content: Content) -> some View {
        HStack
        {
            content
            if !text.isEmpty {
                Button(
                    action: {
                        self.text = ""
                        
                    })
                {
                    Image(systemName: "delete.left")
                        .foregroundColor(Color(UIColor.opaqueSeparator))
                }
                .padding(.trailing, 8)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

参考になったサイト

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?