Swift UIView无需任何输入即可调整其框架的大小

时间:2019-05-26 05:30:39

标签: ios swift iphone uiview swift4

我正在尝试创建一个模仿AppStore中卡片行为的应用程序。我当前执行此操作的方法是在选定时创建集合视图单元格(我加载到该单元格的自定义UIView)的副本,将其添加到与该单元格完全相同的框架中的视图,然后从那里展开。但是,即使我能够为视图分配正确的框架,在“ didSelectItemAt”完成后的某个时候,视图也会将其框架更改为原点(0,0),并将最小宽度和高度更改为适合其内容。

我注意到的一件有趣的事情是,我能够对其进行动画处理,并看到它从预期的原点移动到(0,0)。这是如果我使用UIView.animate几秒钟(或将CMD-T用于慢速动画)并在动画块中插入self.view.layoutSubviews()。

    var currentView:AirportView?

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) as? AirportCollectionViewCell {
            whiteView.alpha = 0.3

            currentView = .get()
            self.view.addSubview(currentView!)
            currentView!.frame = self.view.convert(cell.frame, from: self.collectionView)
            print("Relative     frame is \(self.view.convert(cell.frame, from: self.collectionView))")
            print("Current view frame is \(currentView!.frame)")
        }
    }

    @objc func reset(_ sender: UIView) {
        print("Reset cview frame is \(currentView!.frame)")
        whiteView.alpha = 0
        currentView?.removeFromSuperview()
        currentView = nil
        self.collectionView.isScrollEnabled = true
    }

这是打印语句的输出:

Relative     frame is (0.0, 136.0, 375.0, 415.0)
Current view frame is (0.0, 136.0, 375.0, 415.0)
Reset cview  frame is (0.0, 0.0, 323.6666666666667, 458.6666666666667)

如果在分配新框架后立即调用self.view.layoutSubviews(),则输出为:

Relative     frame is (0.0, 136.0, 375.0, 415.0)
Current view frame is (0.0, 0.0, 323.6666666666667, 414.6666666666667)
Reset cview  frame is (0.0, 0.0, 323.6666666666667, 458.6666666666667)

因此,我假设该视图在幕后某个位置布置子视图,但是我不确定为什么这会改变currentView的框架。

任何建议都将不胜感激,这使我整日生气。

0 个答案:

没有答案