You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

禁用UITextField键盘默认工具栏上下箭头按钮的最佳方法

方案1:仅移除上下箭头按钮(保留工具栏其他功能)

iOS 9及以上系统可以通过修改UITextInputAssistantItem属性直接移除默认的上下导航按钮,不会影响文本框的第一响应者逻辑,是侵入性最低的方案。
如果只需移除上下导航箭头,不需要清空trailingBarButtonGroups,保留该配置可以正常使用自动填充、听写等系统自带的工具栏功能。

Swift 代码示例

// 仅移除上下箭头,保留其他工具栏功能
textField1.inputAssistantItem.leadingBarButtonGroups = []
textField2.inputAssistantItem.leadingBarButtonGroups = []

// 如果需要同时移除右侧自带功能按钮,可以加上这行
// textField1.inputAssistantItem.trailingBarButtonGroups = []

Objective-C 代码示例

// 仅移除上下箭头,保留其他工具栏功能
textField1.inputAssistantItem.leadingBarButtonGroups = @[];
textField2.inputAssistantItem.leadingBarButtonGroups = @[];

// 如果需要同时移除右侧自带功能按钮,可以加上这行
// textField1.inputAssistantItem.trailingBarButtonGroups = @[];

方案2:隐藏整个键盘工具栏

如果你完全不需要键盘上方的助手工具栏,也可以直接给文本框设置空的inputAccessoryView,整个工具栏都会被隐藏:

Swift 代码示例

textField1.inputAccessoryView = UIView(frame: .zero)
textField2.inputAccessoryView = UIView(frame: .zero)

Objective-C 代码示例

textField1.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectZero];
textField2.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectZero];

你提到的默认带上下箭头的工具栏就是下图的样式:
键盘示意图

内容的提问来源于stack exchange,提问作者Alec Barton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 21:36:03