简体   繁体   English

当固有尺寸超出可用宽度时,按比例调整布局高度

[英]Proportionally adjust layout height when the intrinsic size overflows the available width

I'm playing with a (macOS) today list widget and I'm using autolayout to size the widget. 我正在使用(macOS)今天的列表小部件,并且正在使用自动布局来调整小部件的大小。 That widget contains a custom NSView, rendering some preformatted text (pretty much like an HTML pre tag). 该小部件包含一个自定义的NSView,呈现一些预格式化的文本(非常类似于HTML pre标签)。

Based on the text being rendered the widget has an intrinsic content size. 基于呈现的文本,小部件具有固有的内容大小。 It also scales properly (down or up) and centeres if the set frame is bigger or smaller than the intrinsic size. 如果设置的框架大于或小于固有尺寸,它也可以正确缩放(向下或向上)并居中。

This generally works well in many scenarios, except one. 除一种情况外,这通常在许多情况下都能正常工作。 When being shown as a Today widget, the width of the widget is constrained to a specific size. 当显示为“今日”窗口小部件时,该窗口小部件的宽度被限制为特定大小。 Which is fine even if the intrinsic size is bigger than the available width, it'll scale down. 即使固有尺寸大于可用宽度,也可以缩小尺寸。 However it seems that autolayout still seems to use the intrinsic height of the widget as-is, despite that the width got constrained. 但是 ,尽管宽度受到限制,但似乎自动布局仍然按原样使用小部件的固有高度。 That way my view appears padded on the height, like so: 这样,我的视图看起来像是填充在高度上,如下所示:

/------------------\
|    extra pad     |
|------------------|
|                  |
|  downsized view  |
|                  |
|------------------|
|    extra pad     |
\------------------/

Example: intrinsic size of view being rendered 800x400, width of today view 400. View gets scaled down propertionally to 400x200, but the layout height still seems to be the original 400 (200 padding at top & bottom). 示例:渲染的固有尺寸为800x400,今天的宽度为400。视图按比例缩小为400x200,但是布局高度似乎仍然是原始的400(顶部和底部为200填充)。

Question: how do I tell autolayout to take the scaling into account. 问题:我如何告诉自动布局将缩放比例考虑在内。 It sounds almost like something like -intrinsicSizeForMaxSize: would be needed. 听起来几乎需要-intrinsicSizeForMaxSize:之类的东西。

I tried setting a width/height ratio constraint, reducing compression resistance and increasing hugging priority, but that doesn't kill the extra padding. 我尝试设置宽度/高度比率约束,降低了压缩阻力并增加了拥抱优先级,但这并没有杀死额外的填充。 I think I'm missing something on how autolayout considers the intrinsic size when it overflows. 我想我缺少关于自动布局在溢出时如何考虑固有大小的信息。

Switched from using an intrinsic size to a ratio constraint. 从使用固有大小转换为比率约束。 I still calculate the 'native' size of the view, but only use that to figure out the width/height ratio. 我仍然计算视图的“本机”大小,但仅使用它来计算宽度/高度比。 Then I add a constraint to keep that ratio. 然后,我添加一个约束来保持该比率。 Seems to work fine. 似乎工作正常。 Here is the relevant code: 以下是相关代码:

  let size  = nativeSize // calculate the desired size
  let ratio = size.height / size.width

  if let rc = ratioConstraint {
    rc.isActive = false
    removeConstraint(rc)
  }

  ratioConstraint =
    NSLayoutConstraint(item:   self, attribute: .height, relatedBy: .equal,
                       toItem: self, attribute: .width,
                       multiplier: ratio, constant: 0)
  ratioConstraint?.isActive = true

Then I just pin the view to the container via |[self]| 然后我通过|[self]|将视图固定到容器 for both horizontal and vertical. 对于水平和垂直。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM