调整 UIView 的大小以便 UIScrollView 可以工作

发布于 2024-12-27 04:32:49 字数 799 浏览 1 评论 0原文

我有一个小表单,主要是在界面生成器中构建的:

在此处输入图像描述

当用户触摸其中一个文本字段时,我使屏幕的顶部可滚动(请注意,当键盘不存在时,它不需要任何滚动,并且两个字段都不会隐藏在键盘后面)。

在此处输入图像描述

我用很少的代码实现了这一点。我的表单控件都位于 UIScrollView 内,该控件在加载时被禁用且大小为 0x0(默认情况下)。当我检测到键盘的存在时,我启用它并将其大小调整为<​​strong>视图的宽度和视图的高度+键盘大小。然后,当键盘被移除时,我禁用滚动视图并调整其大小(我将其设置回 0x0)。

这几乎可以完美地工作。但有一个例外(到目前为止):

可滚动区域(或者更确切地说,其中的视图)太高。因为没有键盘的表单不会占据整个屏幕,所以我发现我可以滚动到视图的底部(一个全屏)+键盘高度,而我真正想要的是滚动到视图的内容 + 键盘高度。

我以为我可以简单地缩短视图,使其大小适合内容,但界面生成器(xcode 4.2)似乎强制它占据整个屏幕。尝试在代码中执行此操作也不起作用,因为显然 self.view.frame.size 不可分配。我怎样才能实现我想要的目标?具体来说,当键盘存在时,我希望屏幕上半部分的可滚动区域的大小调整为底部没有太多“松弛”:

在此处输入图像描述

I have a small form which I built mostly in interface builder:

enter image description here

When the user touches inside one of the textfields, I make the top part of the screen scrollable (note that it does not require any scrolling when the keyboard is not present, and that neither field is hidden behind the keyboard).

enter image description here

I achieve this with very little code. My form controls are all inside a UIScrollView, which is disabled and sized 0x0 (by default) on load. When I detect the keyboard's presence I enable it and resize it to the views's width and the view's height + the keyboard size. I then disable and resize the scrollview when the keyboard is removed (I set it back to 0x0).

This works almost perfectly. With one exception (so far):

The scrollable area (or rather, the view inside it) is too tall. Because the form without the keyboard does not take up the whole screen, I find that I can scroll to the bottom of my view (one full screen) + keyboard height, when what I really want is to scroll to the view's contents + keyboard height.

I thought I could simply make the view shorter so that it is just the right size for the contents, but interface builder (xcode 4.2) seems to force it to take up the whole screen. Attempting to do it in code also doesn't work, because apparently self.view.frame.size is not assignable. How can I achieve what I'm looking for? Specifically, when the keyboard is present, I want the scrollable area in the top half of the screen to be sized such that there isn't so much 'slack' at the bottom:

enter image description here

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

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

发布评论

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

评论(1

薄凉少年不暖心 2025-01-03 04:32:49

首先, self.view.frame 是一个结构体,具有两个字段,这两个字段本身就是结构体 - 大小和起源。如果您想更改大小或原点,则必须更改整个框架

self.view.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y, 
    self.view.frame.size.width, <your desired height>);

其次,使用什么基础来创建表单?这看起来只是一个 UITableVIew。如果是这样,请考虑将 UIViewController 的类型更改为 UITableViewController,因为它会自动处理键盘。

如果两者都不适合您,请提供您在处理键盘外观时使用的代码,以便我们可以想到更具体的答案。

First, self.view.frame is a struct with two fields that are structs itself - size and origin. If you want to change either size or origin, you have change the whole frame

self.view.frame = CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y, 
    self.view.frame.size.width, <your desired height>);

Second, what base do use to create your form? This looks like just a UITableVIew. If so, consider changing type of your UIViewController to UITableViewController, since it handles keyboard automatically.

If neither is an option for you, provide the code you use when handling keyboard appearance, so that we could think of more concrete answer.

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