我创建了一个自定义UIView:
我正在使用NSLayoutConstraints来创建布局和大小。由于UIView
没有内在大小'就像UILabel
或UITextField
一样,我将视图的NSLayoutAttributeBottom
等于加20到最低的子视图。这会强制视图向下和向右延伸,根据其子视图的位置给出大小。
[self addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|[topBarView]-[nameLabel]"
options:0
metrics:nil
views:views]];
NSLayoutConstraint *imageViewBottomConstraint =
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:imageView
attribute:NSLayoutAttributeBottom
multiplier:1 constant:20];
imageViewBottomConstraint.priority = 400;
[self addConstraint:imageViewBottomConstraint];
我将bottom属性设置为imageView(带齿轮图像的蓝框)并设置优先级。
[self addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[nameLabel][nameTextField]-(8@600)-[commentsLabel]-(0@600)-[commentsTextView]"
options:NSLayoutFormatAlignAllLeading
metrics:nil
views:views]];
NSLayoutConstraint *commentsTextViewBottomConstraint =
[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:commentsTextView
attribute:NSLayoutAttributeBottom
multiplier:1 constant:20];
commentsTextViewBottomConstraint.priority = 300;
[self addConstraint:commentsTextViewBottomConstraint];
我还将底部属性设置为评论文本视图,其优先级较低,因此很容易破解。
[self addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-[imageView]-(50)-[nameTextField]"
options:NSLayoutFormatAlignAllTop
metrics:nil
views:views]];
[self addConstraint:[NSLayoutConstraint
constraintWithItem:commentsTextView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nameTextField
attribute:NSLayoutAttributeWidth
multiplier:1 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:nameTextField
attribute:NSLayoutAttributeRight
multiplier:1 constant:20]];
然后我设置正确的属性给它宽度。
我遇到高度为0的问题,直到我将其添加到子视图中:
PhotoDisplayPanel *photoPanel = [[PhotoDisplayPanel alloc] initWithPhotoData:nil];
[self.view layoutIfNeeded];
DLog(@"BEFORE | photoPanel.frame.size: (%f, %f)", photoPanel.frame.size.width, photoPanel.frame.size.height);
[self.view addSubview:photoPanel];
[self.view layoutIfNeeded];
DLog(@"AFTER | photoPanel.frame.size: (%f, %f)", photoPanel.frame.size.width, photoPanel.frame.size.height);
结果:
DEBUG | -[PhotoDisplayPanel initWithPhotoData:] | self.frame.size: (790.000000, 0.000000)
DEBUG | -[LoginViewController viewDidLoad] | BEFORE | photoPanel.frame.size: (790.000000, 0.000000)
DEBUG | -[LoginViewController viewDidLoad] | AFTER | photoPanel.frame.size: (790.000000, 389.000000)
在将视图添加到视图之前,我很困惑为什么视图有宽度而不是高度。这是我在尝试将视图设置为UIPopoverController
时遇到的问题。
编辑:
在玩完之后,原因是我添加到2个底部属性约束的优先级。我删除了' commentsTextViewbottomConstraint'约束并删除了`imageViewBottomConstraint'的优先级,它几乎可以工作:
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topBarView]-[nameLabel]" options:0 metrics:nil views:views]];
NSLayoutConstraint *imageViewBottomConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:imageView attribute:NSLayoutAttributeBottom multiplier:1 constant:20];
// imageViewBottomConstraint.priority = 400;
[self addConstraint:imageViewBottomConstraint];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[nameLabel][nameTextField]-(8@600)-[commentsLabel]-(0@600)-[commentsTextView]" options:NSLayoutFormatAlignAllLeading metrics:nil views:views]];
// NSLayoutConstraint *commentsTextViewBottomConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:commentsTextView attribute:NSLayoutAttributeBottom multiplier:1 constant:20];
// commentsTextViewBottomConstraint.priority = 300;
// [self addConstraint:commentsTextViewBottomConstraint];
新结果:
DEBUG | -[PhotoDisplayPanel initWithPhotoData:] | self.frame.size: (590.000000, 572.000000)
DEBUG | -[LoginViewController viewDidLoad] | BEFORE | photoPanel.frame.size: (590.000000, 572.000000)
DEBUG | -[LoginViewController viewDidLoad] | AFTER | photoPanel.frame.size: (590.000000, 589.000000)
我以这种方式开发了代码,因此如果图像视图比文本视图的底部属性更大,则会拉伸以显示完整图像。或者,如果图像小于文本视图的底部属性,则它将保持显示文本视图的最小高度,例如:
知道如何维护此功能吗?