从较小的UIView Swift创建较大的UIView

时间:2019-01-09 11:58:33

标签: ios swift uiview frame addsubview

我有一个带有滚动视图的屏幕。在滚动视图内部,有一个带有按钮的子视图。我希望单击按钮时,在屏幕底部添加一个较大的子视图,其宽度与屏幕的宽度相同。

现在我现在面临以下问题:

  1. 获取屏幕的高度将返回滚动视图的实际高度,但是我不希望这样,我想要设备上的屏幕高度,因为子视图的位置与滚动视图无关。
  2. 在要添加的子视图的设置框架中,如果将x坐标设置为0,则它​​将其引用为w.r.t坐标。 subview2而不是屏幕。

有关更多信息,请参阅所附图像: Before button click

After Button Click

在单击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中的表视图

0 个答案:

没有答案