【问题标题】:Auto Layout intrinsic content size to fill screen自动布局内在内容大小以填满屏幕
【发布时间】:2014-09-22 19:49:05
【问题描述】:

我有一个使用AVCaptureVideoPreviewLayer 显示设备摄像头馈送的视图。我计划这个视图将填满设备的屏幕并在顶部和底部留出一些空间。

Q1 我的视图是否有意义实现intrinsicContentSize 并让它提供缩放时的大小以使其恰好适合屏幕?还是我误解了自动布局和intrinsicContentSize

Q2 在我的UIView 子类中实现intrinsicContentSize 可以使用[[self window] screen] 吗?如果布局系统在视图附加到屏幕后询问视图调用intrinsicContentSize,这将起作用。如果调用发生在此之前,视图将不知道它将附加到哪个屏幕,这将失败。

【问题讨论】:

    标签: ios uikit autolayout


    【解决方案1】:

    我最终没有为此使用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 填满屏幕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      相关资源
      最近更新 更多