首先问题是这样的,我自定义了一个UIView,刚开始是在LayoutSubView的方法里先设置了子控件的frame,然后又根据最有一个子控件的大小设置整个frame的大小,代码如下:-(void) layoutSubviews
{
[self.productImgScrollView setFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_WIDTH)];
[self.productNameLabel setFrame:CGRectMake(15, _productImgScrollView.frame.size.height+18, DEVICE_WIDTH-15*2, 18)];
[self.productPriceLabel setFrame:CGRectMake(15, self.productNameLabel.frame.origin.y+self.productNameLabel.frame.size.height+18, DEVICE_WIDTH-15*2, 18)];
if(self.product.discount!=nil)//有打折时
{
[self.productCountLabel setFrame:CGRectMake(15, self.productPriceLabel.frame.origin.y+self.productPriceLabel.frame.size.height+10, DEVICE_WIDTH-15*2, 13)];
[self createFeatureView:_productCountLabel.frame.origin.y+_productCountLabel.frame.size.height+20];
}else//无打折时
{
[self createFeatureView:_productPriceLabel.frame.origin.y+_productPriceLabel.frame.size.height+20];
}
//设置brandBar
[self createBrandToolBar:self.featureOne.frame.origin.y+self.featureOne.frame.size.height+20];
[self setFrame:CGRectMake(0, 0, DEVICE_WIDTH, self.brandToolBar.frame.origin.y+self.brandToolBar.frame.size.height)]; }
嗯,就是上面这个方法的最后一句,没有在initWithFrame这个方法中设置frame的大小。然后在ViewController里初始化这个自定义的视图(使用的是init方法,也没有设置frame的大小,因为考虑到在layoutsubview方法中已经把frame设置了),然后addSubview到控制器的视图中。但是在添加试图后,使用这个View的.frame.size方法得到的frame的size为(0,0)。此时视图可以正常显示,一切正常。
然后做了尝试,在自定义UIView的InitWithFrame方法中,对frame进行初始化,这是在控制器中是可以获得frame的size的大小的(也就是我在initWithFrame中设置的值),此时感觉问题应该是在我将自定义的view添加到控制器之后,打印frame的大小,此时可能还没有执行layoutsubview的方法,所以还没有设置frame的size的尺寸。
然后问题来了,我尝试在自定义View的initWithFrame和LayoutSubview的方法中都不设置frame的大小,此时,在控制其中,视图竟然也能正常显示,设置背景色显示正好是子控件的大小。
问题1:这里我没有设置frame的大小啊,怎么也能正常显示呢,在layoutsubview中打印frame的大小也是(0,0),这是怎么回事,是父视图可以根据子视图的尺寸自动调整大小吗?
问题2:如果我想根据子控件的位置及大小动态设置父视图的大小,在哪个方法中设置frame的尺寸呢?还是不设置显示都没有问题。
问题3:如果我在sublayoutSubView中设置frame的大小,那么我在控制器中什么时候才能得到执行完sublayuoutSubView这个方法后,frame的尺寸。
Custom controls generally only need to constrain the sub-controls relative to the constraints of your custom control.
The size of the custom control is controlled when you use it. You can also give it a default size.
"How can it be displayed normally if the frame size is not set?"?
Because your width and height (DEVICE_WIDTH-15*2, 18) in
layoutsubview
中设置子视图的frame是有固定高度的 比如productPriceLabel
all have values (even if the size of self is {0,0}).If you want to dynamically set the size of the parent view based on the position and size of the child control, then it is best not to frame it in
layoutsubview
中设置self
. You can notify the management class of the parent view when the subview changes (for example, if you add the parent view to the ViewController's view, then adjust the frame of the parent view in the controller).is still not recommended in the
layoutsubview
中设置self
的frame。另外控制器有个viewDidLayoutSubviews
frame. In addition, the controller has aviewDidLayoutSubviews
method.Question 1, I still don’t understand it. Although I set the size of the sub-control, it is loaded on the custom control. The custom control does not set the size. Why is it OK?
Question 2, I also considered this method of @馒头君, but because I wanted to display it when it was first loaded, some proxy methods were executed after loading, so there was a problem with the display. The method that comes to mind is to execute the proxy of the custom control in viewContrllor
Question 3 I used the layoutIfNeed method. I will try to use the viewDidLayoutSubviews method next time