swift - 以编程方式自动布局(NSLayoutConstraint): center inner view in outer view and constrain to min(width, 高度)

标签 swift autolayout nslayoutconstraint

我不确定我在使用 NSLayoutConstraint 生成自动布局时做错了什么。

我想使用外部 View 的宽度和高度中较小的一个,将内部 View 置于外部 View 的中心。最后,我想应用一个比例因子。

Auto Layout Centered

这是我的代码。

     NSLayoutConstraint.activate([
            inner.centerXAnchor.constraint(equalTo: outer.centerXAnchor),
            inner.centerYAnchor.constraint(equalTo: outer.centerYAnchor),
            inner.widthAnchor.constraint(equalTo: outer.heightAnchor, multiplier: imageScale),
            inner.widthAnchor.constraint(equalTo: outer.widthAnchor, multiplier: imageScale),
            inner.heightAnchor.constraint(equalTo: inner.widthAnchor)
    ])

这适用于:

  • 宽度==高度
  • 宽度<高度

但是当 width > height 我得到这个:

enter image description here

我错过了什么?使用框架很容易做到这一点:

let innerWidth = min(outer.frame.width, outer.frame.height) * imageScale
let innerHeight = innerWidth
inner.frame = CGRect(x: (outer.frame.width -innerWidth) / 2, 
                     y: (outer.frame.height - innerHeight) / 2,
                     width: innerWidth, height: innerHeight)

最佳答案

首先,您在代码中两次设置 inner.widthAnchor 约束有点奇怪。仅设置一次。

此外,您需要根据实际外部 View 的框架为内部 View 的尺寸选择外部 anchor 。如果最小的外部尺寸是宽度,则将内部 View 的宽度限制为 outer.widthAnchor * imageScale。否则,当最小外部尺寸为高度时,您将限制为 outer.heightAnchor * imageScale

这对我来说很有效,可以得到你正在寻找的布局(我刚刚创建了一个简单的单 View 项目):

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let inner = UIView()
        inner.backgroundColor = .yellow
        inner.translatesAutoresizingMaskIntoConstraints = false

        let outer = UIView()
        outer.backgroundColor = .blue
        outer.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(outer)
        view.addSubview(inner)

        // get these from somewhere, e.g. outerWidth.frame.size
        let outerWidth = CGFloat(200)
        let outerHeight = CGFloat(400)
        let outerAnchor: NSLayoutDimension

        if outerWidth >= outerHeight {
            outerAnchor = outer.heightAnchor
        } else {
            outerAnchor = outer.widthAnchor
        }

        let imageScale = CGFloat(0.5)

        NSLayoutConstraint.activate([
            outer.widthAnchor.constraint(equalToConstant: outerWidth),
            outer.heightAnchor.constraint(equalToConstant: outerHeight),
            outer.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            outer.centerYAnchor.constraint(equalTo: view.centerYAnchor),

            inner.centerXAnchor.constraint(equalTo: outer.centerXAnchor),
            inner.centerYAnchor.constraint(equalTo: outer.centerYAnchor),
            inner.widthAnchor.constraint(equalTo: outerAnchor, multiplier: imageScale),
            inner.heightAnchor.constraint(equalTo: inner.widthAnchor)
        ])
    }
}

关于swift - 以编程方式自动布局(NSLayoutConstraint): center inner view in outer view and constrain to min(width, 高度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43011289/

相关文章:

ios - Swift:解除模态后在 ViewController 上执行一个函数

具有相对宽度/高度的 IOS8 自动布局

ios - 安全地更改 NSLayoutConstraint 常量值

ios - 自定义 TableView 单元格出列 fatal error : unexpectedly found nil

swift - 存储一个 Swift 函数及其参数值并在以后调用它

ios - 带有自动布局的 UIScrollView 失败,并在启动时消失

ios - 激活约束 : and deactivateConstraints: not persisting after rotation for constraints created in IB

ios - UIButton 图像即使有约束也会自动调整大小

ios - Siri 与自定义意图的集成面临问题

ios - AutoLayout 约束以适应矩形内的 View ,保留一定的纵横比(以编程方式)