如何让 UIButton 粘在 UIView 的底部
很抱歉,如果这个问题对您来说看起来很愚蠢,但实际上:
我需要在 UIView 上放置一个按钮,其中包含 UITableView 作为其子视图。现在,我已经使用 Interface Builder 将其放在 UITableView 的正下方(通过使 UITableView 短一点,以容纳 UIButton)。
当我编译和运行程序时,Button 保留在表格下方,但我必须滚动 UITableView,因为表格视图包含的行数超出了 iPhone 屏幕可以容纳的行数。
UIButton 是否有任何属性,通过设置它我可以使其出现在视图的底部,而不管 UITableView 的高度如何。 IB有与此相关的内容吗?
提前致谢 :)
Sorry if this question looks dumb to you, but really:
I need to put a button on UIView, which contains UITableView as its subview. Right now, I have put it right below the UITableView using Interface Builder (By making UITableView a bit shorter, to accommodate UIButton).
As I compile and run the program, Button remains under the table, but I have to scroll the UITableView, as the table view contains more than number of rows, that iPhone screen can accommodate.
Is there any property of UIButton, by setting which I can make it appear at the bottom of the view, regardless of UITableView's height. Is there anything related to this in IB ?
Thanks in Advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您有以下布局:
因此该按钮是 UITableView 的子视图。由于它也是一个滚动视图,因此按钮将随之滚动。要使其保持在同一位置,您需要将其设为同级:
I assume you have the following layout:
So the button is a subview of the
UITableView
. Since it is also a scroll view, the button will scroll with it. To make it stay at the same location, you need to make it a sibling:我认为你想要的是创建一个带有两个子视图(表视图和按钮)的父视图。您使表格视图框架与父视图框架相同,然后设置按钮的原点,以便它显示在父视图的底部。添加表视图后,您需要将按钮添加为子视图,以便它显示在顶部。然后想让表格视图/滚动视图知道底部被覆盖,这样它就可以让你向下滚动一点。不幸的是,我手头没有语法......
I think what you want is to create a parent view with two subviews, the tableview and the button. You make the tableview frame the same as the parent view frame and then you set the origin of the button so that it appears at the bottom of the parent view. You need to add the button as a subview after you add the table view so it'll appear on top. Then want to let the table view/scroll view know that the bottom part is covered up so it'll let you scroll a little bit further down. Unfortunately, I don't have the syntax for that at hand ...
就像 DarkDust 所说,您至少需要使按钮成为表视图的同级。如果您想让按钮保留在多个视图上,可以尝试以下操作:
此代码会将按钮添加为应用程序窗口的子视图,因此它将保留在那里,直到您显式删除它:
将按钮添加为 应用程序窗口的子视图无论其他物体或视图如何移动,窗口都将使其保持在其位置。
Like DarkDust said, you need to make the button at least a sibling of the table view. You could, if you want to have the button remain over multiple views, try this:
This code will add your button as a subview of the application's window, so it will remain there until you explicitly remove it:
Adding your button as a subview of the window will allow it to remain in its position regardless of the movement of other objects or views.
您确定按钮位于 UIView 内部而不是 UITableView 中吗?在界面生成器中拖动按钮很容易导致表视图包含一个按钮。
Are you sure the button is inside the UIView and not in the UITableView? Dragging the button in interface builder could easily result in a button contained by the tableview.