UIViewからUILabel制約を取得したいが、制約を得ることができない。 私はこのようなCustomView.mに制約を設定します。あなたはNSArray
UIViewから制約を取得する方法プログラムによって
NSArray *constraintArr = self.backButton.constraints;
NSLog(@"cons : %@",constraintArr);
に制約を取得することができますし、インスタンスを設定することができますViewController.m
で- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_titleLabel = [UILabel new];
[self addSubview:_titleLabel];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
_titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *titleLabelBottom = [NSLayoutConstraint constraintWithItem:_titleLabel
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0];
[self addConstraints:@[titleLabelBottom]];
...more code
}
CustomView *view = [CustomView alloc] initWithFrame:viewFrame];
NSLog(@"%@",view.titleLabel.constraints); // nil
'layoutSubviews:'は何度も呼び出され、常に新しい制約を作成します。制約の作成を初期化子 – tgyhlsb