Mic*_*ael 4 ios autolayout xcode6 ibdesignable
我创建了一个自定义的IB可设计视图(请参阅下面的代码),它在IB中正确呈现,并且在运行时也能正常工作.但是,在获取有关视图错位的警告时,我无法在Interface Builder中手动调整视图大小(当触摸调整大小手柄时,视图将在其容器中跳转).
我对各种不同的布局都有相同或类似的行为.如果我在这里做错了,或者这只是IB中的一个错误,你有没有想法?
(PS:我不能忽视警告)

编辑:添加约束的截图:

这是代码(标题):
IB_DESIGNABLE
@interface AKATestView : UIView
@end
Run Code Online (Sandbox Code Playgroud)
执行:
@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
Run Code Online (Sandbox Code Playgroud)
尝试删除self.translatesAutoresizingMaskIntoConstraints = NO;您的createSubviews方法.IB似乎依靠这种翻译来对设计师进行正确的测量.我有完全相同的问题,这解决了它.
我还是translatesAutosizingMaskIntoConstraints要NO为子视图.我确认即使使用此设置也不会产生任何额外的约束YES.希望你也是如此!
| 归档时间: |
|
| 查看次数: |
294 次 |
| 最近记录: |