我对各种不同的布局都有相同或类似的行为.如果我在这里做错了,或者这只是IB中的一个错误,你有没有想法?
(PS:我不能忽视警告)
编辑:添加约束的截图:
这是代码(标题):
IB_DESIGNABLE @interface AKATestVIEw : UIVIEw @end
执行:
@interface AKATestVIEw() @property(nonatomic)BOol subvIEwsCreated; @property(nonatomic)BOol subvIEwConstraintsCreated; @property(nonatomic)NSDictionary* vIEws; @end @implementation AKATestVIEw - (ID)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self setupAfterInit]; } return self; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setupAfterInit]; } return self; } - (voID)setupAfterInit { [self createSubvIEws]; } - (voID)createSubvIEws { if (!self.subvIEwsCreated) { self.translatesautoresizingMaskIntoConstraints = NO; UILabel* labelVIEw = [[UILabel alloc] initWithFrame:CGRectZero]; labelVIEw.text = @"name"; labelVIEw.translatesautoresizingMaskIntoConstraints = NO; [self addSubvIEw:labelVIEw]; UITextFIEld* textFIEld = [[UITextFIEld alloc] initWithFrame:CGRectZero]; textFIEld.borderStyle = UITextborderStyleRoundedRect; textFIEld.placeholder = @"Enter some text"; textFIEld.translatesautoresizingMaskIntoConstraints = NO; [self addSubvIEw:textFIEld]; UILabel* errorMessageLabel = [[UILabel alloc] initWithFrame:CGRectZero]; errorMessageLabel.text = @"Error message"; errorMessageLabel.translatesautoresizingMaskIntoConstraints = NO; [self addSubvIEw:errorMessageLabel]; self.vIEws = @{ @"label": labelVIEw,@"editor": textFIEld,@"errorMessageLabel": errorMessageLabel }; self.subvIEwsCreated = YES; [self setNeedsUpdateConstraints]; } } - (voID)updateConstraints { if (!self.subvIEwConstraintsCreated) { NSDictionary* metrics = @{ @"pt": @(4),@"pr": @(4),@"pb": @(4),@"pl": @(4),@"labelWIDth": @(100),@"errorPl": @(4 + 100 + 4),@"hsLabelEditor": @(4),@"vsEditorError": @(2) }; NSArray* specs = @[ @{ @"format": @"H:|-(pl)-[label(labelWIDth)]-(hsLabelEditor)-[editor]-(pr)-|",@"options": @(NSLayoutFormatAlignAllFirstBaseline) },@{ @"format": @"V:|-(pt)-[editor]-(vsEditorError)-[errorMessageLabel]-(pb)-|",@"options": @(NSLayoutFormatAlignAllLeading|NSLayoutFormatAlignAllTrailing) } ]; for (NSDictionary* spec in specs) { Nsstring* format = spec[@"format"]; NSUInteger options = ((NSNumber*)spec[@"options"]).unsignedIntegerValue; NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:options metrics:metrics vIEws:self.vIEws]; [self addConstraints:constraints]; } self.subvIEwConstraintsCreated = YES; } [super updateConstraints]; } @end解决方法 尝试删除self.translatesautoresizingMaskIntoConstraints = NO;在您的createSubvIEws方法中. IB似乎依靠这种翻译来对设计师进行正确的测量.我有完全相同的问题,这解决了它.
对于子视图,我仍然将“自动化任务矩阵约束”转换为“否”.我确认即使将此设置为YES,也不会产生任何额外的约束.希望你也是如此!
总结以上是内存溢出为你收集整理的ios – 错误的视图警告和使用自动布局的IB_DESIGNABLE自定义视图的奇怪行为全部内容,希望文章能够帮你解决ios – 错误的视图警告和使用自动布局的IB_DESIGNABLE自定义视图的奇怪行为所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)