根据视图添加约束';s高度iOS

根据视图添加约束';s高度iOS,ios,objective-c,autolayout,Ios,Objective C,Autolayout,我有几个子视图,它们根据超级视图的大小进行布局。我在这里使用自动布局,但超级视图的大小始终为0,代码如下: - (instancetype)initWithFrame:(CGRect)frame Count:(NSUInteger)count { self = [super initWithFrame:frame]; if (self) { self.count = count; const float circleHeight = s

我有几个子视图,它们根据超级视图的大小进行布局。我在这里使用自动布局,但超级视图的大小始终为0,代码如下:

- (instancetype)initWithFrame:(CGRect)frame Count:(NSUInteger)count
{
    self = [super initWithFrame:frame];

    if (self)
    {
        self.count = count;

        const float circleHeight = self.bounds.size.height * (float)4 / (5 * self.count - 1);
        NSLog(@"selfHeight %f",self.bounds.size.height);        

        for (UIImageView * circle in self.circleViewArray)
        {
            [self addSubview:circle];
        }

        for (int i = 0;i < self.count;i++)
        {
            [self.circleViewArray[i] mas_makeConstraints:^(MASConstraintMaker * make){
                make.width.equalTo(self);
                make.height.equalTo(self).multipliedBy((float)4 / (5 * self.count - 1));
                make.centerX.equalTo(self);
                make.bottom.equalTo(self).with.offset(-i * (circleHeight + stickHeight));
            }];
        }
    }

    return self;

}
-(instancetype)initWithFrame:(CGRect)帧计数:(NSUTEGER)计数
{
self=[super initWithFrame:frame];
如果(自我)
{
self.count=计数;
const float circhlehight=self.bounds.size.height*(float)4/(5*self.count-1);
NSLog(@“selfHeight%f”,self.bounds.size.height);
用于(UIImageView*自循环数组中的圆)
{
[自添加子视图:圆];
}
for(int i=0;i
请注意,这里我使用第三方砌体来简化代码。 当我在控制台中打印“selfHeight”时,输出总是0。我该怎么处理?

所以你的偏移永远不会起作用,因为它们是根据零计算的,永远不会更新。您需要以某种方式使约束相对,或者在每次帧更改时删除并更新约束(布局需要更新)

使约束相对更好,在您的案例中,您应该考虑将视图链接在一起,以便设置yhen之间的间距,并调整高度以适应全部可用高度

伪代码:

UIView *previousView = nil;

for (view in views) {

    view.leading = super.leading;
    view.trailing = super.trailing;

    if (previousView) {
        view.top = previousView.bottom;
        view.height = previousView.height; // equal relation, not static height
    } else {
        view.top = super.top;
    }

    previousView = view;
}

previousView.bottom = super.bottom;

你从哪里得到要传递的帧?我只是向方法传递了一个CGRectZero,因为我在它的超级视图中自动布局了它。你的圆视图应该如何排列?你能添加一个屏幕截图/草图吗?如果
bounds
CGRectZero
,那么我建议检查你传递给
帧的内容,并确保它不是
CGRectZero
。你会看到,如果你在视图控制器的
viewdiload
或类似的过程中抓取
frame
。实际上我将一个帧传递给父视图,我知道它们是根据零计算的,但我只是想知道为什么高度为零。进行计算时,self的宽度和高度都为零。那么我如何使用这里的自我宽度和高度?不要。将视图固定在边缘和彼此之间,并允许宽度和高度由布局工程师确定我不太了解。你能给我看一下设置约束的代码吗?