- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我知道自动布局链基本上包含 3 个不同的过程。
我不太清楚-setNeedsLayout
和-setNeedsUpdateConstraints
之间的内在区别。来自 Apple 文档:
Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews. This method makes a note of the request and returns immediately. Because this method does not force an immediate update, but instead waits for the next update cycle, you can use it to invalidate the layout of multiple views before any of those views are updated. This behavior allows you to consolidate all of your layout updates to one update cycle, which is usually better for performance.
When a property of your custom view changes in a way that would impact constraints, you can call this method to indicate that the constraints need to be updated at some point in the future. The system will then call updateConstraints as part of its normal layout pass. Updating constraints all at once just before they are needed ensures that you don’t needlessly recalculate constraints when multiple changes are made to your view in between layout passes.
当我想在修改约束后为 View 设置动画并为我通常调用的更改设置动画时:
[UIView animateWithDuration:1.0f delay:0.0f usingSpringWithDamping:0.5f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self.modifConstrView setNeedsUpdateConstraints];
[self.modifConstrView layoutIfNeeded];
} completion:NULL];
我发现如果我使用 -setNeedsLayout
而不是 -setNeedsUpdateConstraints
一切都会按预期工作,但是如果我更改 -layoutIfNeeded
使用 -updateConstraintsIfNeeded
,动画不会发生。
我试图做出自己的结论:
-updateConstraintsIfNeeded
只更新约束但不强制布局进入进程,因此仍保留原始帧-setNeedsLayout
也调用 -updateContraints
方法那么什么时候可以用一个代替另一个呢?关于布局方法,我需要在约束发生变化的 View 或父 View 上调用它们吗?
最佳答案
你的结论是对的。基本方案是:
setNeedsUpdateConstraints
确保将来对 updateConstraintsIfNeeded
的调用会调用 updateConstraints
。setNeedsLayout
确保将来对 layoutIfNeeded
的调用调用 layoutSubviews
。当调用 layoutSubviews
时,它也会调用 updateConstraintsIfNeeded
,因此根据我的经验,很少需要手动调用它。事实上,除了调试布局时,我从未调用过它。
使用 setNeedsUpdateConstraints
更新约束也很少见,objc.io–a must read about autolayouts–says :
If something changes later on that invalidates one of your constraints, you should remove the constraint immediately and call setNeedsUpdateConstraints. In fact, that’s the only case where you should have to trigger a constraint update pass.
此外,根据我的经验,我从来不需要使约束无效,也不必在代码的下一行设置 setNeedsLayout
,因为新的约束几乎要求新的布局。
经验法则是:
setNeedsLayout
。updateConstraints
方法中的约束(顺便说一句,这是更改约束的推荐方法),请调用 setNeedsUpdateConstraints
,大多数时候,setNeedsLayout
之后。layoutIfNeeded
附加它。另外,在您的动画代码中,我相信 setNeedsUpdateConstraints
是不需要的,因为约束是在动画之前手动更新的,并且动画只会根据旧 View 和旧 View 之间的差异重新布局 View 新的。
关于ios - setNeedsLayout 与 setNeedsUpdateConstraints 和 layoutIfNeeded 与 updateConstraintsIfNeeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20609206/
不太明白为什么layoutIfNeeded仍然没有返回布局的正确宽度...但是如果我通过调度获得宽度,它将返回正确的宽度。看来layoutIfNeeded 不会立即布局,而是异步的。如果我错了请纠正我
出现崩溃: navigationController.view.layoutIfNeeded() log : fatal error: unexpectedly found nil while unw
我有一个表格 View ,我使用此方法计算单元格高度 override func tableView(_ tableView: UITableView, heightForRowAt indexPat
所以,我有这样的代码 UIView.animate(withDuration: 2) { self.navigationController?.navigationBar.isHidden =
我有一个 UITextView,正在显示文本当文本不是很大时(我说的是 100-200 个单词),应用程序运行得很好 如果文本非常大(大约 10000 个单词),它就会不断崩溃 代码 func scr
我有一个按钮,可以根据某些情况改变大小。我不需要完成动画,因为用户永远看不到发生的变化。有时,当从较小尺寸切换回较大尺寸时,按钮会卡在较小尺寸上。我认为 layoutIfNeeded() 可以解决这个
我正在为 UITextField 的大小调整设置动画。我正在使用带有约束的自动布局。我的 Storyboard 中有 H:|-[textField] -(8)-[label]-(8)-[image]-
我正在使用此功能使用大量渐变绘图: func drawGradient(colors: [CGColor], locations: [NSNumber]) { let gradientLay
何时以及如何使用 layoutIfNeeded?我知道当我们更改 View 的布局时,我们可以调用 setNeedsLayout 来更新布局,但不确定何时应该使用 layoutIfNeeded。 注意
我正在 xib 文件中创建以下 View 。 有两个约束,我一次想要其中一个,所以我激活一个并停用另一个约束。 其中一个约束是 View (顶层 View )view.bottom == inputV
我正在尝试使用自动版式在屏幕上和屏幕外为我的 ADBannerView 制作动画。预自动布局(设置框架和动画)一切正常。现在我有以下代码,它在 viewDidLoad 上运行以确保广告横幅是否离开屏幕
我已经以编程方式构建了 View ,然后为它们设置了动画。有一个 View 动画链,最后一个是要设置动画的按钮。 当我通过调用 layoutIfNeeded() 更改约束(这些约束独立于其他约束并且没
我遇到了一个让我很困惑的问题。我写了一个简单的两张图片的动画,效果很好。但是突然间它停止了动画,我不明白为什么。我没有做任何我能想到的会影响动画的改变。我已经检查过 NSLayoutConstrain
UIView 我有两种状态: 我在它们之间使用 @IBaction 为 button 制作动画: @IBAction func tapped(sender: UIButton) { flag
一些 S.O.问题展示了一种自动布局技术,用于确定 View 满足其约束所需的最小尺寸:[header systemLayoutSizeFittingSize: UILayoutFittingComp
我这里有一个用于自定义 tableview 单元格的类,但在 awakeFromNib 中,self.frame 始终为 0,0,0,0。我尝试调用 [self layoutIfNeeded] 但这没
layoutIfNeeded 在 iPad 上无法正常工作,但在 iPhone 上可以正常工作。我想要一个圆形按钮。在 iPhone 上圆圈生成正确,但在 iPad 上它看起来像菱形。 _btnthu
我有一个适用于 iPad 的数学程序,它使用 Big Number 库进行计算,然后根据这些计算更新 UIView (myView) 中最多包含 20,000 个 UILabel 的数组。 计算可能需
我注意到的奇怪的事情: let navController = UINavigationController(rootViewController: myRootViewController) nav
我的主视图有一个 subview ,我使用以下扩展添加了一个掩码: extension UIView{ func addTopRoundCorners(){ let bezierPath =
我是一名优秀的程序员,十分优秀!