我的代码中有两个类,一个是NameCell
,它包含一个带有文本的简单UILabel。第二个是继承自该类的NameValueCell
,但也添加了属性UIView *valueView
。
需要更改一个布局约束。我正在寻找一种方法来覆盖:
H:|[nameView]|
- nameView
应在NameCell
中占据全角
使用
在NameValueCell
中,H:|[nameView][valueView(==nameView)]|
- nameView
与valueView
的宽度比例应为1:1
覆盖NSLayoutConstraint的最佳实践是什么?我必须在代码中坚持继承,因为我的应用程序需要许多不同的UITableViewCell专门化。
NameCell.h
@interface NameCell : UITableViewCell
@property (nonatomic, retain) IBOutlet UIView *nameView;
@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@end
名称ValueCell.h:
@interface NameValueCell : NameCell
@property (nonatomic, retain) IBOutlet UIView *valueView;
@end
NameCell.com:
@implementation NameCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIView *nameView = [[UIView alloc] init];
self.nameView = nameView;
[self.contentView addSubview:self.nameView];
UILabel *nameLabel = [[UILabel alloc] init];
self.nameLabel = nameLabel;
[self.nameView addSubview:self.nameLabel];
NSDictionary *views = NSDictionaryOfVariableBindings(nameView, nameLabel);
NSArray *constraints;
// The constraint that should be overridden
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameView]|"
options: 0
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
}
return self;
}
@end
NameValueCell.m:
@implementation NameValueCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSString *reuseID = reuseIdentifier;
UIView *valueView = [[UIView alloc] init];
self.valueView = valueView;
[self.contentView addSubview:self.valueView];
NSDictionary *views = @{
@"nameView": self.nameView,
@"nameLabel": self.nameLabel,
@"valueView": self.valueView
};
NSArray *constraints;
// The overriding constraint
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameView][valueView(==nameView)]|"
options: 0
metrics:nil
views:views];
[self.contentView addConstraints:constraints];
}
return self;
}
@end
发布于 2018-06-19 01:17:04
子类希望增强超类行为,例如,通过添加额外的子视图;但它也希望在创建约束时覆盖超类。要做到这两点,最好的方法是分解视图创建和约束创建代码,然后在子类中,通过有选择地调用super
来控制是增强还是覆盖。
首先,考虑因素...
// in NameCell.m initWithStyle
// super initWithStyle..., if (self) { ...
[self addCustomSubviews];
[self addCustomConstraints];
在NameCell
中,这些新方法应该完全按照问题中的内联方式实现,但在子类中实现:(1)根本不实现init,允许超类init调用分解后的代码,以及(2)覆盖分解后的代码,如下所示……
// NameValueCell.m
- (void)addCustomSubviews {
// augmenting super here, so call super...
[super addCustomSubviews];
// add the value view
UIView *valueView = [[UIView alloc] init];
// and so on
}
- (void)addCustomConstraints {
// overriding super here, so don't call super, just add constraints
NSDictionary *views = @{
// and so in
}
// in NameValueCell.m, in the initWithStyle method, before creating constraints
[self removeConstraints:self.constraints]; // then ...
NSDictionary *views = @{ // and so on...
我不认为这是一种最佳的(甚至是好的)实践,但我认为它应该是可行的。
发布于 2018-06-19 07:30:07
第一:不要添加约束;激活它们。它要简单得多,也不容易出错。
那好吧。只需在NSArray实例变量中保留对可能需要替换的约束的引用:
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameView]|"
options: 0
metrics:nil
views:views];
self.removeableConstraints = constraints; // an instance property
[NSLayoutConstraint activateConstraints: constraints];
现在,子类要做的就是停用self.removeableConstraints
并激活它的替换约束。
[NSLayoutConstraint deactivateConstraints: self.removeableConstraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameView][valueView(==nameView)]|"
options: 0
metrics:nil
views:views];
[NSLayoutConstraint activateConstraints: constraints];
这是换出约束的一般模式,这里的类/子类关系没有理由不使用它。
https://stackoverflow.com/questions/50913608
复制相似问题