最後我沒有使用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
通過在故事板中設置其寬度約束來填滿屏幕。