Dodawanie przycisku" Wyczyść "do iPhone' a UITextField

Jak dodać mały przycisk" X " po prawej stronie pola UITextField, który czyści tekst? Nie mogę znaleźć atrybutu do dodania tej sub-kontroli w Interface Builder w iPhone OS 2.2 SDK.

Uwaga: W Xcode 4.x i nowsze (iPhone 3.0 SDK i nowsze), możesz to zrobić w Interface Builder.

Author: Kristopher Johnson, 2008-11-26

9 answers

Ten przycisk jest wbudowaną nakładką, która jest dostarczana przez klasę UITextField, ale od iPhone OS 2.2 SDK, nie ma żadnego sposobu, aby ustawić go za pomocą Interface Builder. Musisz włączyć go programowo.

Dodaj gdzieś ten wiersz kodu (na przykład viewDidLoad):

myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;

Aktualizacja: najnowsza wersja Xcode IB obsługuje tę właściwość.

 284
Author: Kristopher Johnson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-27 15:57:37

Możesz również ustawić to bezpośrednio z narzędzia Interface Builder pod Inspektorem atrybutów.

Tutaj wpisz opis obrazka

Zaczerpnięte z XCode 5.1

 42
Author: Nicholas Harlen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-30 02:30:25

Możesz dodać niestandardowy przycisk Wyczyść i kontrolować rozmiar i każdą rzecz za pomocą tego:

UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
[clearButton setImage:img forState:UIControlStateNormal];
[clearButton setFrame:frame];
[clearButton addTarget:self action:@selector(clearTextField:) forControlEvents:UIControlEventTouchUpInside];

textField.rightViewMode = UITextFieldViewModeAlways; //can be changed to UITextFieldViewModeNever,    UITextFieldViewModeWhileEditing,   UITextFieldViewModeUnlessEditing
[textField setRightView:clearButton];
 32
Author: Hossam Ghareeb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-06 10:47:35

Swift 3:

textField.clearButtonMode = UITextFieldViewMode.whileEditing

Lub jeszcze krócej:

textField.clearButtonMode = .whileEditing
 29
Author: Esqarrouth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-07 15:57:32

Objective C:

self.txtUserNameTextfield.myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;

Swift:

txtUserNameTextfield.clearButtonMode = UITextFieldViewMode.WhileEditing;
 7
Author: PT Vyas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-03 10:45:25

To nie działa, rób jak ja:

Swift:

customTextField.clearButtonMode = UITextFieldViewMode.Always

customTextField.clearsOnBeginEditing = true;

func textFieldShouldClear(textField: UITextField) -> Bool {
    return true
}
 4
Author: Tritmm,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-23 02:51:56

Na Xcode 8 (8A218a):

Swift:

textField.clearButtonMode = UITextFieldViewMode.whileEditing;

Litera "W"przeszła z wielkiej litery na "w".

 4
Author: Aidan.C,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-21 08:40:29
  func clear_btn(box_is : UITextField){
    box_is.clearButtonMode = .always
    if let clearButton = box_is.value(forKey: "_clearButton") as? UIButton {
        let templateImage =  clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate)

        clearButton.setImage(templateImage, for: .normal)
        clearButton.setImage(templateImage, for: .highlighted)

        clearButton.tintColor = .white

     }
}
 1
Author: Pardeep Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-28 04:13:41

Na Xcode w wersji 8.1 (8B62) można to zrobić bezpośrednio w Inspektorze atrybutów. Wybierz pole tekstowe, a następnie wybierz odpowiednią opcję z listy rozwijanej Wyczyść przycisk, która znajduje się w Inspektorze atrybutów.

 -4
Author: carlson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-05 14:14:28