ios - 为什么当我将 UIStackView 分布设置为 equalspacing 时 subview 不可见?

标签 ios swift autolayout uistackview

我想将一组自定义 View 水平放置在 UIStackView 中。 每个 subview 都有一个宽度和高度等于常量约束:

 let stack = UIStackView(frame: frameRect)
 stack.axis = UILayoutConstraintAxis.horizontal
 stack.distribution = UIStackViewDistribution.equalSpacing
        
 stack.layoutMargins = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40)
 stack.isLayoutMarginsRelativeArrangement = true

 for i in 1...3 {
     let numberView = RotatingNumberView()
     numberView.heightAnchor.constraint(equalToConstant: CGFloat(cellSize))
     numberView.widthAnchor.constraint(equalToConstant: CGFloat(cellSize))
     numberViews.append(numberView)
     stack.addArrangedSubview(numberView)
  }

如果我更改为 fillEqually 分布,则会显示 subview ,但它们的宽度会被拉伸(stretch)以填充 stackView。

我猜它与拥抱或压缩阻力优先级有​​关,但在 subview 中设置这些不起作用。

这里发生了什么?

最佳答案

对于除 fillEqually 之外的所有分布,堆栈 View 在计算其沿堆栈轴的大小时使用每个排列 View 的 intrinsicContentSize 属性。

因此覆盖此属性将起作用。

  override var intrinsicContentSize: CGSize { return CGSize(your values) }

关于ios - 为什么当我将 UIStackView 分布设置为 equalspacing 时 subview 不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44127089/

相关文章:

objective-c - 核心图像 CIPerspectiveTransform 过滤器 : How to use CIVectors?

swift - 什么时候在 Swift 中使用 `protocol` 和 `protocol: class`?

初始化中的 Swift 动态属性

iphone - 以编程方式自动布局不起作用

ios - TableView 单元格对齐导致顶部有一些空间

ios - 带有 Apple Watch 的 Cordova 应用程序无法在 Apple Store 上载(有多个 bundle 与 CFBundleIdentifier 冲突)

Objective-C : Is there any callback invoked when sending a message to the object that can't respond?

android - RubyMotion - 联合 iOS/Android 开发

ios - 表格 View 单元格上的步进器(快速)

ios - UIScrollView 子项的尾随空格限制错误