ios - 自动布局动画以中心为 anchor

标签 ios swift3 autolayout ios-animations

我有一个基于约束的高度动画。但是,当动画开始时,它将 View 的中心作为 anchor 而不是顶部。

我希望它动画为固定顶部并从底部收缩。

对不起,我的英语不是母语人士。

我的观点主播:

top: superview
left: superview
right: superview
bottom: nil

// current height constant 200
view.height.constant = 0

UIView.animation(withDuration: 0.2, animations: {
    view.layoutIfNeded()
}}

最佳答案

尝试代替你的观点:

superView.layoutIfNeeded()

这将布局 subview 并且您的动画应该可以工作。

所以替换

    // current height constant 200
view.height.constant = 0

UIView.animation(withDuration: 0.2, animations: {
    view.layoutIfNeded()
}

    // current height constant 200
yourView.height.constant = 0

UIView.animation(withDuration: 0.2, animations: {
    yourViewSuperview.layoutIfNeded()
}

请参阅 layoutIfNeeded() 苹果文档:[1]https://developer.apple.com/documentation/uikit/uiview/1622507-layoutifneeded

关于ios - 自动布局动画以中心为 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45344067/

相关文章:

ios - 如何将 AWS-S3 加密文件(AES-256 加密)读取到我的 IOS 应用程序中?

ios - 在 Swift 中使用 NSNotificationCenter 在选项卡栏 Controller 之间传递数据

ios - Cocos2d-x AdMob iOS 集成错误

ios - Firebase runTransactionBlock 在 swift 3 中不起作用

ios - info.plist中的网络身份验证

ios - 以编程方式调整 UITextField 的大小会自动恢复为旧大小

ios - View 约束——适合一个 subview

ios - 快速确定委托(delegate)属性的类别

swift3 - 无法在 Swift 3 错误中减去两个 CGPoint 操作数

swift - 在 Swift 中将 MPMoviePlayerController 正确放置在 UIView 中