添加“清除"iPhone UITextField 的按钮

Adding the quot;Clearquot; Button to an iPhone UITextField(添加“清除iPhone UITextField 的按钮)
本文介绍了添加“清除"iPhone UITextField 的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 UITextField 右侧添加用于清除文本的小X"按钮?我在 iPhone OS 2.2 SDK 的 Interface Builder 中找不到用于添加此子控件的属性.

How do you add that little "X" button on the right side of a UITextField that clears the text? I can't find an attribute for adding this sub-control in Interface Builder in the iPhone OS 2.2 SDK.

注意:在 Xcode 4.x 及更高版本(iPhone 3.0 SDK 及更高版本)中,您可以在 Interface Builder 中执行此操作.

Note: In Xcode 4.x and later (iPhone 3.0 SDK and later), you can do this in Interface Builder.

推荐答案

这个按钮是由 UITextField 类提供的内置叠加层,但在 iOS 2.2 SDK 中,没有'没有任何方法可以通过 Interface Builder 设置它.您必须以编程方式启用它.

This button is a built-in overlay that is provided by the UITextField class, but as of the iOS 2.2 SDK, there isn't any way to set it via Interface Builder. You have to enable it programmatically.

在某处添加这行代码(例如viewDidLoad):

Add this line of code somewhere (viewDidLoad, for example):

Objective-C

myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;

Swift 5.0

myUITextField.clearButtonMode = .whileEditing

这篇关于添加“清除"iPhone UITextField 的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)