【问题标题】:Create a UIView and grow it to fill screen创建一个 UIView 并将其增长以填满屏幕
【发布时间】:2018-01-05 23:33:33
【问题描述】:

我正在制作一个包含两个主要视图的天气。 第一个是主视图,它有一个可滚动的 UICollectionView,其中有代表小型天气卡的单元格。第二个是不同的视图,其中显示了更详细的所有天气细节。

我想在集合视图之上创建一个 UIView 并将其扩大以填满屏幕。可悲的是,我一直失败,找不到合适的方法来做到这一点。 当单元格被按下时,我想在单元格顶部创建一个 UIView(获取它在视图上的位置),然后将其增长以填充屏幕。

我打算把代码放在哪里:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("Cell [\(indexPath.row)] selected.")

    // Get cell position on screen:
    let attributes = collectionView.layoutAttributesForItem(at: indexPath)
    let cellRect = attributes?.frame
    let frameCellInSuperview = collectionView.convert(cellRect!, to: collectionView.superview)
    print("x: \(Double(frameCellInSuperview.origin.x)), y: \(Double(frameCellInSuperview.origin.y))")

    // Create a small UIView in the exact same position


    // Grow the UIView to fill the screen
}

【问题讨论】:

    标签: ios iphone uiview swift3 uicollectionview


    【解决方案1】:

    您可以使用此代码针对 superview 获取当前索引帧:

       let theAttributes:UICollectionViewLayoutAttributes! = collectionView.layoutAttributesForItem(at: indexPath)
       let cellFrameInSuperview:CGRect!  = collectionView.convert(theAttributes.frame, to: collectionView.superview)
    

    现在创建一个相同框架的 UIView 。使用此代码:

    var popView = UIView(frame: cellFrameInSuperview)
    self.view.addSubview(popView)
    

    现在通过更新框架动画视图:

    UIView.animateWithDuration(3.0, animations: {
            popView.frame = UIScreen.main.bounds
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 2012-11-02
      • 2015-06-06
      相关资源
      最近更新 更多