Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在寻找一种方法,让 CALayer 在其子层发生变化时调整自身大小(这意味着当任何子层的边界发生变化或子层数组本身发生变化时)。
当我之前使用视图时,我通过在我的自定义 UIView 子类中实现 sizeThatFits 来管理它,只要视图的子视图发生变化,它就会由 sizeToFit 自动调用。
由于 CALayer 具有 sizeThatFits-equivalent-method preferredSize,我很惊讶没有找到 sizeToFit-equivalent。
我认为您需要实现(覆盖) - (void)layoutSublayers ... *子类可以覆盖它以 * 提供自己的布局算法,它应该设置 * 每个子层的框架
- (void)layoutSublayers
- (void)layoutSublayers { // self.frame = aNewSubLayer.bounds // basically do algorithm for setting frame and bounds for this(self) layer and subLayers here }