Autolayout 不适用于 centerX-centerY
Autolayout not working on centerX- centerY
我正在尝试手动添加约束;每次代码尝试添加 centerX 和 centerY 约束时,我都会遇到以下错误:
Terminating app due to uncaught exception 'NSGenericException',
reason: 'Unable to install constraint on view. Does the constraint
reference something from outside the subtree of the view? That's
illegal. constraint:
(active)> view:>'
我检查了之前的多个问题,但没有任何帮助:/
谢谢
代码
[cell.contentView addSubview:imageView];
[cell.contentView addSubview:title];
title.translatesAutoresizingMaskIntoConstraints = NO;
imageView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *imageViewHeightConstraint = [NSLayoutConstraint
constraintWithItem:imageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:[UIScreen mainScreen].bounds.size.width/8];
NSLayoutConstraint *imageViewWidthtConstraint = [NSLayoutConstraint
constraintWithItem:imageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:imageView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
NSLayoutConstraint *imageViewCenterXConstraint = [NSLayoutConstraint
constraintWithItem:imageView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:[imageView superview]
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0];
NSLayoutConstraint *imageViewCenterYConstraint = [NSLayoutConstraint
constraintWithItem:imageView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:[imageView superview]
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0];
[imageView addConstraint:imageViewHeightConstraint];
[imageView addConstraint:imageViewWidthtConstraint];
[imageView addConstraint:imageViewCenterXConstraint];
[imageView addConstraint:imageViewCenterYConstraint];
你为什么不使用:
imageView.center = cell.center
它应该使您的图像在单元格中居中。
尝试将 centerX 和 centerY 约束添加到 imageView 的父视图,而不是 imageView
[cell.contentView addConstraint:imageViewCenterXConstraint];
[cell.contentView addConstraint:imageViewCenterYConstraint];
请在为 UIImageView 添加约束后尝试此操作。
对于Objective C
[cell.contentView layoutIfNeeded];
[self.view layoutIfNeeded];
对于Swift
cell.contentView.layoutIfNeeded()
self.view.layoutIfNeeded()
请先尝试这个,先给出约束,然后将 imageView 添加到单元格 contentView。
删除 addConstraint
行并在约束的每个实例化之后添加 .active = YES
。同时将 [image superview]
更改为 cell
我正在尝试手动添加约束;每次代码尝试添加 centerX 和 centerY 约束时,我都会遇到以下错误:
Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint: (active)> view:>'
我检查了之前的多个问题,但没有任何帮助:/
谢谢
代码
[cell.contentView addSubview:imageView];
[cell.contentView addSubview:title];
title.translatesAutoresizingMaskIntoConstraints = NO;
imageView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *imageViewHeightConstraint = [NSLayoutConstraint
constraintWithItem:imageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:[UIScreen mainScreen].bounds.size.width/8];
NSLayoutConstraint *imageViewWidthtConstraint = [NSLayoutConstraint
constraintWithItem:imageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:imageView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
NSLayoutConstraint *imageViewCenterXConstraint = [NSLayoutConstraint
constraintWithItem:imageView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:[imageView superview]
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0];
NSLayoutConstraint *imageViewCenterYConstraint = [NSLayoutConstraint
constraintWithItem:imageView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:[imageView superview]
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0];
[imageView addConstraint:imageViewHeightConstraint];
[imageView addConstraint:imageViewWidthtConstraint];
[imageView addConstraint:imageViewCenterXConstraint];
[imageView addConstraint:imageViewCenterYConstraint];
你为什么不使用:
imageView.center = cell.center
它应该使您的图像在单元格中居中。
尝试将 centerX 和 centerY 约束添加到 imageView 的父视图,而不是 imageView
[cell.contentView addConstraint:imageViewCenterXConstraint];
[cell.contentView addConstraint:imageViewCenterYConstraint];
请在为 UIImageView 添加约束后尝试此操作。
对于Objective C
[cell.contentView layoutIfNeeded];
[self.view layoutIfNeeded];
对于Swift
cell.contentView.layoutIfNeeded()
self.view.layoutIfNeeded()
请先尝试这个,先给出约束,然后将 imageView 添加到单元格 contentView。
删除 addConstraint
行并在约束的每个实例化之后添加 .active = YES
。同时将 [image superview]
更改为 cell