swift 中的设置框架未按预期工作
setting frame in swift doesn't work as expected
我正在将应用程序从 Objective-C 移植到纯 Swift,但我遇到了一个奇怪的问题。
我有 AlertView
class,它是标准 UIAlertView
(现在是 UIAlertController
)的替代品,用于显示动画弹出窗口。 AlertView
在 UIViewController
扩展中创建 - 它由视图控制器的视图框架启动并添加为子视图。
AlertView
有一个 属性,它是 PopupView
的实例 - 自定义 UIView
subclass 和 xib(在自动布局上)。此弹出窗口应具有动态高度,具体取决于其内容(多行消息标签)。
现在,当我尝试在 AlertView
class 中设置此弹出窗口的动画时:
- 当我在
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)
编辑2
setTranslatesAutoresizingMaskIntoConstraints(false)
setTranslatesAutoresizingMaskIntoConstraints(true)
好的,找到解决方案了。我已经停止混合自动布局和直接框架修改,而是使用纯自动布局。
我正在将应用程序从 Objective-C 移植到纯 Swift,但我遇到了一个奇怪的问题。
我有 AlertView
class,它是标准 UIAlertView
(现在是 UIAlertController
)的替代品,用于显示动画弹出窗口。 AlertView
在 UIViewController
扩展中创建 - 它由视图控制器的视图框架启动并添加为子视图。
AlertView
有一个 属性,它是 PopupView
的实例 - 自定义 UIView
subclass 和 xib(在自动布局上)。此弹出窗口应具有动态高度,具体取决于其内容(多行消息标签)。
现在,当我尝试在 AlertView
class 中设置此弹出窗口的动画时:
- 当我在
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)
编辑2
setTranslatesAutoresizingMaskIntoConstraints(false)
setTranslatesAutoresizingMaskIntoConstraints(true)
好的,找到解决方案了。我已经停止混合自动布局和直接框架修改,而是使用纯自动布局。