小编Jes*_*sse的帖子

如何以编程方式从UIView获取约束

我想从UIView获得UILabel约束,但我不能得到任何约束.我在CustomView.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

}
Run Code Online (Sandbox Code Playgroud)

在ViewController.m中

 CustomView *view = [CustomView alloc] initWithFrame:viewFrame];
 NSLog(@"%@",view.titleLabel.constraints); // nil
Run Code Online (Sandbox Code Playgroud)

objective-c uiview ios autolayout nslayoutconstraint

8
推荐指数
1
解决办法
8894
查看次数