I'm getting some strange behavior.
UIStackView *stackView = [UIStackView alloc];
stackView = [stackView initWithArrangedSubviews:@[self.subview1,self.subview2]];
[self addSubview:stackView];
[stackView setTranslatesAutoresizingMaskIntoConstraints:NO]
[self addConstraint:[NSLayoutConstraint constraintWithItem:stackView attribute: NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0f constant:0]]
This code runs fine when I'm using the Ipad Air 2 simulator from XCode, but when I actually try to run it on my Ipad Air 2, it crashes when trying to add the layout constraint (last line of the code), complaining that stackView
is nil
.
Upon stepping through the code, I actually found that stackView
is nil
after the very first line, the call to [UIStackView alloc]
. This is strange to me, as I don't know why an alloc call would return nil
, much less why it would return nil
only on an actual device instead of the simulation of a device.
Edit:
I originally had the code as follows:
UIStackView * stackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.subview1,self.subview2]];
[self addSubview:stackView];
[stackView setTranslatesAutoresizingMaskIntoConstraints:NO]
[self addConstraint:[NSLayoutConstraint constraintWithItem:stackView attribute: NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0f constant:0]]
In this instance, stackView
was nil
after the combined alloc
/init
call, but I don't know which one made it nil
. Thus why I separated out the calls to see.