【问题标题】:Custom UIView from XIB loading with huge scale for subviews来自 XIB 的自定义 UIView 加载具有巨大规模的子视图
【发布时间】:2016-02-18 19:32:00
【问题描述】:

我遇到了自定义 UIView 实现的问题。我想要做的是为 UICollectionViewCell 设置动画,并在单元格缩放和“翻转”后创建一个新视图。 (将 UICollectionViewCells 视为卡片)。

我成功地实现了所需的动画。但是,当我在 XIB 文件的代码中创建视图时,似乎应用了约束,但一切都被放大了。我将标签的最小字体比例设置为 0.2,但它似乎不起作用。

这是在 IB 中设计的视图:

View designed in IB with constraints

现在,当动画继续向单元格视图添加新的 CardBackView 时,我得到了以下结果:

The horrendous result

这是执行动画并添加卡片后视图的代码:

// now flip card and show other side of card
            UIView.transitionWithView(cell, duration: duration, options: [.BeginFromCurrentState, .TransitionFlipFromRight, .CurveEaseInOut], animations: { () -> Void in

                let cardBackView = NSBundle.mainBundle().loadNibNamed("CardBackView", owner: self, options: nil).first as! CardBackView
                cardBackView.frame = cell.bounds
                cardBackView.titleLabel.text = movie.title
                cardBackView.titleLabel.textColor = UIColor.whiteColor()
                cardBackView.overviewLabel.text = movie.overview
                cardBackView.backgroundColor = UISettings.TabBarColor
                cell.addSubview(cardBackView)

                }, completion: nil)

【问题讨论】:

    标签: ios swift animation uiview uicollectionviewcell


    【解决方案1】:

    当您将 CardBackView 添加到 Cell 时,它假定您将自动调整大小的掩码转换为约束,因此它的行为可能会很奇怪。要解决此问题,您需要为 CardBackView 创建约束并将它们添加到您的 Cell(它是父级)。

    如果您的 CardBackView 应该使用与您的 Cell 相同的大小,那么这样的事情应该可以工作:

    cardBackView.translatesAutoresizingMaskIntoConstraints = false
    cell.addConstraint(NSLayoutConstraint(item: cardBackView, attribute: .Top, relatedBy: .Equal, toItem: cell, attribute: .Top, multiplier: 1, constant: 0))
    cell.addConstraint(NSLayoutConstraint(item: cardBackView, attribute: .Bottom, relatedBy: .Equal, toItem: cell, attribute: .Bottom, multiplier: 1, constant: 0))
    cell.addConstraint(NSLayoutConstraint(item: cardBackView, attribute: .Left, relatedBy: .Equal, toItem: cell, attribute: .Left, multiplier: 1, constant: 0))
    cell.addConstraint(NSLayoutConstraint(item: cardBackView, attribute: .Right, relatedBy: .Equal, toItem: cell, attribute: .Right, multiplier: 1, constant: 0))
    

    【讨论】:

      猜你喜欢
      • 2013-10-08
      • 1970-01-01
      • 2015-02-03
      • 2020-05-16
      • 2017-10-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 2012-10-23
      相关资源
      最近更新 更多