gpt4 book ai didi

ios - 在继承的类中覆盖 NSLayoutConstraint

转载 作者:行者123 更新时间:2023-12-01 19:43:57 27 4
gpt4 key购买 nike

我的代码中有两个类,一个是NameCell ,其中包含一个带有文本的简单 UILabel。第二个是NameValueCell ,它继承自该类,但还添加了属性 UIView *valueView .

需要更改一个布局约束。我正在寻找一种方法来覆盖:
H:|[nameView]| - nameView应该占据 NameCell 中的全宽


H:|[nameView][valueView(==nameView)]| - nameViewvalueView NameValueCell 中的宽度比应为 1:1

覆盖 NSLayoutConstraint 的最佳实践是什么?我必须坚持在我的代码中继承,因为我的应用程序需要许多不同的 UITableViewCell 特化。

NameCell.h:

@interface NameCell : UITableViewCell

@property (nonatomic, retain) IBOutlet UIView *nameView;
@property (nonatomic, retain) IBOutlet UILabel *nameLabel;

@end

NameValueCell.h:
@interface NameValueCell : NameCell

@property (nonatomic, retain) IBOutlet UIView *valueView;

@end

NameCell.m:
@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

最佳答案

子类想要增强父类(super class)的行为,例如,通过添加额外的 subview ;但它也想在创建约束时覆盖父类(super class)。要做到这两点,最好的方法是将 View 创建和约束创建代码分解出来,然后在子类中,通过调用 super 来控制我们是增加还是覆盖。选择性地。

首先,排除...

// in NameCell.m initWithStyle
// super initWithStyle..., if (self) { ...
[self addCustomSubviews];
[self addCustomConstraints];

NameCell ,这些新方法应该完全按照您在问题中内联的方式实现,但在子类中:(1)根本不实现 init,允许父类(super class) 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
}

在一个不太繁琐但不太清晰的替代方案中,您可以将您的 init 保持原样,但在子类 init 中,删除刚刚在 super ...
// in NameValueCell.m, in the initWithStyle method, before creating constraints
[self removeConstraints:self.constraints]; // then ...
NSDictionary *views = @{ // and so on...

我不会将这种替代方法称为最佳(甚至是好的)做法,但我认为它应该有效。

关于ios - 在继承的类中覆盖 NSLayoutConstraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50913608/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com