ios - swift 中的设置框架无法按预期工作

标签 ios swift autolayout

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

我有 AlertView 类,它是标准 UIAlertView(现在是 UIAlertController)的替代品,用于显示动画弹出窗口。 AlertViewUIViewController 扩展中创建 - 它由 View Controller 的 View 框架启动并添加为 subview 。

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

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

  • 当我在 PopupView 中设置时 setTranslatesAutoresizingMaskIntoConstraints(false) - View 的高度是正确的,但在动画中设置它的框架并没有按预期工作 - View 粘在顶部左角
  • 当我设置 setTranslatesAutoresizingMaskIntoConstraints(true) - 动画按预期工作但 View 的大小来自 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)

编辑2

setTranslatesAutoresizingMaskIntoConstraints(假)

set-false

setTranslatesAutoresizingMaskIntoConstraints(真)

set-true

最佳答案

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

关于ios - swift 中的设置框架无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29804798/

相关文章:

ios - 以编程方式删除 UIBarButtonItem 的图像

iphone - 是否可以将 AutoLayout 与 UITableView 的 tableHeaderView 一起使用?

iphone - Objective-c:来自 UIView 的 presentViewController

iphone - 设置对象 nil 与 release+realloc

iOS 7 - 单击时正确选择/取消选择行

php - Xcode, PHP;使用 SwiftyJSON-master 从 PHP/MySQL 检索数据时遇到问题

iphone - 释放变量时接收者类型无效

ios - Google Maps iOS SDK 热图未呈现

ios - 当我收到警告时,如何找到 *在哪里* 不明确的布局?

ios - 为什么 iOS 13 放弃我的布局约束更改?