nsomar / OAStackView

Porting UIStackView to iOS 7+
MIT License
2.14k stars 200 forks source link

Crash when adding a new arranged subview #83

Open zeusent opened 8 years ago

zeusent commented 8 years ago

So, first of all let me say thank you! This little fallback solution for UIStackView is awesome.

I've been trying to use it but I do have a problem. I create my OAStackView in Interface Builder where I pre-populate it with some subviews. But then, in code I want to add some more views and I thought that the method insertArrangedSubview:atIndex is what I need.

The problem is that when I do that it crashes. Here's a screen shot of the line that causes the crash:

screen shot 2016-02-26 at 11 23 39

As you can see it seems that self.mutableArrangedSubviews is empty and I've looked all over your code to see where it get's populated, if it ever does, and I can't really find the place that does that.

So, do you think this is a bug or is there a way you can help me?

Thanks

zeusent commented 8 years ago

Ok, I found a quick fix. It seems that adding these lines:

for (UIView *view in self.subviews) {
    [self.mutableArrangedSubviews addObject:view];
}

in initWithCoder: will fix my issue. So here's my complete implementation of that method:

- (instancetype)initWithCoder:(NSCoder *)decoder {
    self = [super initWithCoder:decoder];

    if (self) {
        [self commonInitWithInitalSubviews:@[]];

        if ([NSStringFromClass([self class]) isEqualToString:@"UIStackView"]) {
            self.axis = [decoder decodeIntegerForKey:@"UIStackViewAxis"];
            self.distribution = [decoder decodeIntegerForKey:@"UIStackViewDistribution"];
            self.alignment = [decoder decodeIntegerForKey:@"UIStackViewAlignment"];
            self.spacing = [decoder decodeDoubleForKey:@"UIStackViewSpacing"];
            self.baselineRelativeArrangement = [decoder decodeBoolForKey:@"UIStackViewBaselineRelative"];
            self.layoutMarginsRelativeArrangement = [decoder decodeBoolForKey:@"UIStackViewLayoutMarginsRelative"];
        }

        for (UIView *view in self.subviews) {
            [self.mutableArrangedSubviews addObject:view];
        }

        [self layoutArrangedViews];
    }

    return self;
}
delebedev commented 8 years ago

It looks like regression caused by my fix of KVO: https://github.com/oarrabi/OAStackView/commit/fb7d74110aee1de8b6780ddf91428b3eaced1753

:( no idea how to solve your issue correctly at the moment