我有一个带有滚动视图的屏幕。在滚动视图内部,有一个带有按钮的子视图。我希望在单击按钮时,在屏幕底部添加一个更大的子视图,其宽度与屏幕的宽度相同。
现在我目前面临以下问题:
- 获取屏幕高度返回滚动视图的实际高度,但我不希望这样,我想要设备上屏幕的高度,因为子视图的放置与滚动视图无关。
- 在设置要添加的子视图的框架时,如果我将 x 坐标设置为 0,它会将其引用到子视图 2 而不是屏幕的坐标。
单击 Button1 时,需要添加 Subview3。
编辑 1:在 subview2 中,我正在处理 button1 的点击。单击时,我尝试了:
subview3.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
self.view.addSubview(subview3)
subview3.expandFromBottom()
然后在expandFromBottom(),我有:
tableHeightConstraint?.constant = min(CGFloat(self.items.count) * self.itemHeight - 1, UIScreen.main.bounds.height)
print("before \(self.view.frame)")
print("before \(self.view.convert(view.frame.origin, to: UIApplication.shared.keyWindow))")
UIView.animate(withDuration: 0.5, animations: { [unowned self] () -> Void in
self.view.frame.origin.y -= (self.tableHeightConstraint?.constant)!
})
print("\(self.view.frame)")
print("\(self.view.convert(view.frame.origin, to: UIApplication.shared.keyWindow))")
这打印了以下值:
before (0.0, 0.0, 414.0, 736.0)
before (18.0, 860.0)
(0.0, -155.0, 414.0, 736.0)
(18.0, 550.0)
并且 subview3 在屏幕上不可见。
早些时候,我在添加 subview3 时尝试将 0 作为 y 坐标,但这使得视图从 subview2 的原点坐标出现。此外,早些时候我使用 UIApplication.shared.keyWindow 来获取屏幕的高度,我返回的高度包括滚动视图的高度,但这个问题似乎通过使用 UIScreen.main.bounds 得到解决。
PS:在 expandFromBottom 中,我正在尝试添加从屏幕底部出现的视图动画。
PS2: tableHeightConstraint 用于 subview3 内的 tableview