【发布时间】:2019-07-10 20:48:45
【问题描述】:
我在 UIScrollView 中有一个表格和一个按钮,我想要以下设计:
- 按钮至少在表格最后一行下方 40 pts
- 按钮总是在视图末端上方 83 pts
通过以下限制,我设法使按钮始终在表格最后一行下方 40 pts,并且仅当表格足够长时才在视图末尾上方 83 pts。在我看来,bottomConstraint 的优先级没有正确覆盖topConstraint 的约束。我已将滚动视图设置为包含整个屏幕。
/* - Sign Out button is 40 pts tall - */
let heightConstraint = NSLayoutConstraint(item: signOutBtn, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 41)
/* - Sign Out button is ALWAYS 83 pts above bottom of screen, when visible - */
let bottomConstraint = NSLayoutConstraint(item: signOutBtn, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1, constant: -83)
bottomConstraint.priority = UILayoutPriority.required
/* - Sign Out button is AT LEAST 40 pts below last row of table - */
let topConstraint = NSLayoutConstraint(item: signOutBtn, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: tableView, attribute: .bottom, multiplier: 1, constant: 40)
topConstraint.priority = UILayoutPriority.defaultLow
/* - Sign Out button stretches across the screen - */
let leadingConstraint = NSLayoutConstraint(item: signOutBtn, attribute: .leading, relatedBy: .equal, toItem: scrollView, attribute: .leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: signOutBtn, attribute: .trailing, relatedBy: .equal, toItem: scrollView, attribute: .trailing, multiplier: 1, constant: 0)
scrollView.addConstraints([heightConstraint, bottomConstraint, leadingConstraint, trailingConstraint, topConstraint])
如果表格太长,退出按钮不会出现,用户需要向下滚动到它。
【问题讨论】:
-
这和你之前的问题一样吗? stackoverflow.com/questions/56972736/…
-
@DonMag 我提出了一个新问题,因为这让我感到困惑,为什么我添加的约束优先级似乎没有任何效果,而之前的问题是问我应该采取什么方法来实现这种效果.
-
这仍然令人困惑...根据您的图像,您希望在表格最后一行下方 40 点处有一个按钮。那么视图底部上方的 83 点是什么?或者,当表格很短时,您是否希望按钮位于视图底部上方 83 分(因此底行和按钮之间的间隙可能为 300 分),直到 tableView 增长到“将其推离屏幕”?
-
@DonMag 我明白你的困惑。我已经更新了这个问题,我希望它能解决问题......第一个屏幕截图是我使用已有的约束看到的。剩下的就是我想看到的了。所以我想要一个总是在视图底部上方 83 pts 的按钮,并且随着 tableView 的增长,按钮被按下,在它和表格的最后一行之间保持 40 pts 的间距。
-
我在对您之前的问题的评论中发布了一个链接,该链接正是您正在尝试做的事情:“这是一种方法:stackoverflow.com/a/56134043/6257435 .. . 在该示例中,只需将“页脚视图”(这是一个普通的 UIView)替换为您的按钮。”
标签: ios swift uitableview uiscrollview