我正在将一个应用程序从 Objective-C 移植到纯 Swift 中,但我遇到了一个奇怪的问题。
我有 AlertView
类,它是标准 UIAlertView
(现在是 UIAlertController
)的替代品,用于显示动画弹出窗口。 AlertView
在 UIViewController
扩展中创建 - 它由 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(假)
setTranslatesAutoresizingMaskIntoConstraints(真)
最佳答案
好的,找到解决方案了。我已经停止混合自动布局和直接框架修改,而是使用纯自动布局。
关于ios - swift 中的设置框架无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29804798/