gpt4 book ai didi

ios - 使用自动布局的 IB_DESIGNABLE 自定义 View 的错位 View 警告和奇怪行为

转载 作者:可可西里 更新时间:2023-11-01 05:11:51 24 4
gpt4 key购买 nike

我创建了一个自定义的 IB 可设计 View (参见下面的代码),它在 IB 中正确呈现并且在运行时也能正常工作。但是,在收到有关 View 放错位置的警告时,我无法在 Interface Builder 中手动调整 View 大小(触摸调整大小 handle 时, View 将在其容器中跳来跳去)。

对于各种不同的布局,我会得到相同或相似的行为。您是否知道我在这里做错了什么,或者这只是 IB 中的一个错误?

(PS:我不能忽略警告)

Misplaced view warning

编辑:添加约束截图:

Constraints

这是代码(标题):



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

最佳答案

尝试在 createSubviews 方法中删除 self.translatesAutoresizingMaskIntoConstraints = NO;。 IB 似乎是依靠这个翻译来对设计师进行正确的测量。我有完全相同的问题,这解决了它。

对于 subview ,我仍然有 translatesAutosizingMaskIntoConstraintsNO。我确认即使将此设置为 YES 也不会生成任何额外的约束。希望您也是如此!

关于ios - 使用自动布局的 IB_DESIGNABLE 自定义 View 的错位 View 警告和奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29167965/

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