最后我没有使用intrinsicContentSize
。看起来更简单也更灵活,以允许CameraView
在我的故事板中控制其大小。
我CameraView
,一个UIView
子类有一个aspectRatioConstraint
属性,它建立和更新其updateConstraints
覆盖:
-(void) updateConstraints
{
if(!_aspectRatioConstraint)
{
const CGSize videoSize = [self videoSize];
NSLayoutConstraint *const aspectConstraint =
[NSLayoutConstraint constraintWithItem: self
attribute: NSLayoutAttributeWidth
relatedBy: NSLayoutRelationEqual
toItem: self
attribute: NSLayoutAttributeHeight
multiplier: videoSize.height/videoSize.width
constant: 0];
[self setAspectRatioConstraint: aspectConstraint];
[self addConstraint: aspectConstraint];
}
[super updateConstraints];
}
(约束的比率由回
[self videoSize]
什么相机设备报告确定)
在故事板中,我还在相机的视图上设置了宽高比约束,但我检查约束上的placeholder
选项,以便在构建时将其删除。我将其设置为4:3,这可能与真实相机可能相同或不同 - 它只是在视图布局过程中给我提供了正确的框架。
我使CameraView
通过在故事板中设置其宽度约束来填满屏幕。