我有一个带有滚动视图的屏幕。在滚动视图内部,有一个带有按钮的子视图。我希望单击按钮时,在屏幕底部添加一个较大的子视图,其宽度与屏幕的宽度相同。
现在我现在面临以下问题:
在单击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坐标,但这会使视图从原坐标w变成subview2。另外,之前我使用UIApplication.shared.keyWindow来获取屏幕的高度,并且我返回的高度包括滚动视图的高度,但是该问题似乎可以通过使用UIScreen.main.bounds来解决。
PS::在expandFromBottom中,我试图添加从屏幕底部出现的视图的动画。
PS2: tableHeightConstraint用于子视图3中的表视图