【问题标题】:setting frame in swift doesn't work as expected在 swift 中设置框架不能按预期工作
【发布时间】:2015-04-22 17:24:43
【问题描述】:

我正在将一个应用程序从 Objective-C 移植到纯 Swift 中,但遇到了奇怪的问题。

我有 AlertView 类,它是标准 UIAlertView(现在是 UIAlertController)的替代品,用于显示动画弹出窗口。 AlertView 是在 UIViewController 扩展中创建的 - 它使用视图控制器的视图框架初始化并添加为子视图。

AlertView 有一个属性,它是PopupView 的实例 - 带有 xib 的自定义 UIView 子类(在自动布局上)。此弹出窗口应具有动态高度,具体取决于其内容(多行消息标签)。

现在,当我尝试在 AlertView 类中为这个弹出窗口设置动画时:

  • 当我设置 PopupView setTranslatesAutoresizingMaskIntoConstraints(false) - 视图的高度是正确的,但在动画中设置它的框架不能按预期工作 - 视图粘在左上角
  • 当我设置 setTranslatesAutoresizingMaskIntoConstraints(true) - 动画按预期工作但视图的大小来自 xib(不会根据内容展开)

这里有什么问题?

编辑 显示弹出方法:

private func showPopup(popupView: PopupView)
{
    var beginFrame = popupView.frame
    beginFrame.origin.y = -beginFrame.size.height
    beginFrame.origin.x = self.bounds.size.width/2 - beginFrame.width/2
    popupView.frame = beginFrame

    var endFrame = beginFrame
    endFrame.origin.y = self.bounds.size.height/2 - endFrame.size.height/2

    popupView.hidden = false
    DLog(beginFrame)

    UIView.animateWithDuration(kAnimationTime, delay: 0, usingSpringWithDamping: kAnimationDamping, initialSpringVelocity: kAnimationSpringVelocity, options: UIViewAnimationOptions.CurveEaseIn, animations:
        { () -> Void in
            DLog(endFrame)
            popupView.frame = endFrame
    }, completion: nil)
}

在这两种情况下,它都会显示在控制台中:

(72.5, -155.0, 230.0, 155.0)

(72.5, 256.0, 230.0, 155.0)

EDIT2

setTranslatesAutoresizingMaskIntoConstraints(false)

setTranslatesAutoresizingMaskIntoConstraints(true)

【问题讨论】:

  • @Rahul 情况并非如此-我没有使用标准UIAlertView 也不是'UIAlertController`-我用我的自定义UIView 替换它们并在UIViewController 的扩展中显示逻辑
  • 你能用 liceCap 给我们提供一个 gif 吗? :) cockos.com/licecap
  • @Zolnoor 当然,我已经编辑了我的问题
  • 嗯。那很奇怪。我建议在实例化弹出框和动画时之间的每一行(或接近它)检查它的原点是否在任何点为(0,0)。抱歉,我没有比这更好的建议了

标签: ios swift autolayout


【解决方案1】:

好的,找到解决方案。我已经停止混合使用自动布局和直接帧修改,而是使用纯自动布局。

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多