简体   繁体   English

如何让图层的帧根据其超级层的帧或其视图的帧自动调整大小?

[英]how do i get a layer's frame to automatically resize based on its superlayer's frame or its view's frame?

I'm experimenting with using cagradientlayer to draw gradients in our app instead of having a subclass of uiview manage gradients. 我正在尝试使用cagradientlayer在我们的应用程序中绘制渐变,而不是让uiview的子类管理渐变。 One snafu that i've come across is that when the view that has the gradient as a sublayer of its main layer gets resized to fit the data i am trying to show, the layer doesn't resize along with it. 我遇到的一个问题是,当将具有渐变作为其主图层的子层的视图调整大小以适合我想要显示的数据时,该图层不会随之调整大小。 I end up having the gradient layer end at the original frame size while my view's frame is much larger. 我最终得到了原始帧大小的渐变层结束,而我的视图框架要大得多。

Is there a way to have the sublayer autoresize to fit its superlayer's frame, or the superlayer's view's frame? 有没有办法让子层自动调整以适应其超级层的框架,或超级层的视图框架?

Implement layoutSubviews in the subclass of your view. 在视图的子类中实现layoutSubviews It gets called when the frame is resized (or when setNeedsLayout is called). 调整帧大小时(或调用setNeedsLayout时)调用它。 Just set the layer's frame to the view's bounds: 只需将图层的框架设置为视图的边界:

-(void)layoutSubviews
{
    someSubview.frame = self.bounds;   // make the subview frame match its view
}

这将更好地工作

-(void)viewWillLayoutSubviews

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM