在复杂的 iPhone UI 上完成文本编辑的最佳方式

发布于 2024-09-16 13:26:48 字数 508 浏览 3 评论 0原文

我有一个有点复杂的 iOS 视图层次结构。一段文本是一个可编辑的 UITextField。当用户触摸它时,它成为第一响应者,并且可以编辑。

不过,问题在于:最佳实践应该是,触摸编辑控件之外的任何位置都会导致它放弃第一响应者并结束编辑。实现这一目标的最佳方法是什么?

我尝试过的技术:

  1. 使用 exclusiveTouch 属性,它可以阻止用户与其他控件交互,但不会导致编辑结束。还禁止用户与我的工具栏“完成”按钮进行交互。
  2. 将透明 UIView 放置在文本字段控件下方以及其他所有内容(工具栏除外)之上,然后使用触摸来结束编辑。这是可行的,但我最终将文本字段重新设置为位于整个层次结构之上的另一个随机视图,这意味着我必须在多个位置处理文本字段的布局,因为它不再位于它所在的位置最初,我必须将其所有行为从“盾牌”视图来回委托给其本机主容器,该容器具有所有相关逻辑。

对于我所缺少的这个问题,有一个优雅的解决方案吗?我认为这一定是一个常见的设计问题。

谢谢。

I have a somewhat complex iOS view hierarchy. One piece of text is an editable UITextField. When the user touches it, it becomes first responder, and is editable.

Here's the rub, though: Best practice should be that a touch anywhere outside the edit control causes it to resign first responder and end editing. What's the best way of accomplishing this?

Techniques I've tried:

  1. Use the exclusiveTouch property, which stops the user from interacting with other controls, but doesn't cause editing to end. Also disallows user from interacting with my toolbar "Done" button.
  2. Put a see-through UIView under the text field control and on top of everything else (except the toolbar), and use touches there to end editing. This works, but I end up reparenting the text field onto this other random view which sits above my whole hierarchy, which means I have to take care of the text field's layout in multiple places, since it no longer lives in the place where it lived originally, and I have to delegate all its behavior back and forth from its "shield" view to its native home container, which has all the related logic.

Is there an elegant solution to this problem that I'm missing? I figure it must be a common design issue.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染柒℉ 2024-09-23 13:26:48

在文本视图周围平铺 4 个“透视”视图以捕获/忽略触摸。不需要修改或“提升”文本视图,并且可以以相当模块化的方式添加到父视图。

如果不知道遮罩将覆盖什么以及遮罩不会遮盖什么,则无法遮盖某个区域。因此,任何解决方案都需要足够的范围来收集这两个边界。要么向上传递文本矩形,要么向下传递要禁用的视图矩形/区域,或者两者都传递到中间的某个位置。如果不是文本视图的控制器,则要覆盖的内容的控制器似乎是合并矩形或区域的好地方。

Tile 4 "see-thru" views around the textview to capture/ignore touches. Doesn't require modifying or "lifting" the textview, and can be added to the parent view in a fairly modular way.

You can't mask a region without knowing what that mask will cover and what the mask will not cover. So any solution will require enough reach to gather both of those bounds. Either pass the text rect up, or the view rect/region to be disabled down, or both to something in-between. The controller for the stuff to be covered seems as good a place as any to consolidate both rects or regions, if not the controller for the text view.

世界如花海般美丽 2024-09-23 13:26:48

问题的核心在于什么是“最佳实践”。除非用户关闭键盘,否则键盘会一直保留,这是故意的。例如,许多应用程序需要用户能够在仍在文本字段中工作的同时点击按钮。

键盘有一个返回按钮。 “最佳实践”是通过退出第一响应者来响应用户点击该按钮。否则,您应该将键盘留在那里,因为这是用户所期望的。

但是,如果您坚持按照自己的方式进行操作,有一个简单的解决方案:将 UITapGestureRecognizer 放在背景视图上。如果用户点击背景或界面中的任何按钮或类似按钮,则会触发其处理程序。因此,假设您已记录第一响应者是什么,您可以将 resignFirstResponder 发送到点击手势识别器的处理程序中的第一响应者。

The nub of the issue is what constitutes "best practice". The fact that the keyboard remains unless the user dismisses it is deliberate. For example, many apps need the user to be able to tap a button while still working in a text field.

The keyboard has a Return button. "Best practice" is to respond to the user tapping that button by resigning first responder. Otherwise, you should leave the keyboard there, since that's what the user expects.

However, if you insist on doing it your way, there's a simple solution: put a UITapGestureRecognizer on the background view. Its handler will be triggered if the user taps on the background or on any button or similar in the interface. So, presuming you have kept a record of what the first responder is, you can send resignFirstResponder to the first responder in the tap gesture recognizer's handler.

她说她爱他 2024-09-23 13:26:48

如果您将基本视图更改为 UIControl,您可以向该层添加一个 IBAction,以将您的文本字段作为第一响应者。

另外,如果您有多个触摸事件,请确保它们在触摸时都成为FirstResponder

我很想知道更多细节来验证我的解释 xD

If you change your base view to a UIControl you can add an IBAction to that layer that resigns your text field as first responder.

Also, if you have multiple touch events, make sure they each becomeFirstResponder when touched.

I'd love to have some more details to qualify my explanations xD

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文