SWIFT:当布局具有ScrollView和ImageView时,横向模式下的约束中断

时间:2020-04-09 00:22:36

标签: ios swift

构建一个iOS应用,在该视图中,允许用户捏和缩放图像。 启动时,图像显示适合屏幕。 使用ImageView和ScrollView进行布局,其布局定义如下:

private func setup() {
        scrollView.addSubview(imageView)
        scrollView.delegate = self
        view.addSubview(scrollView)
        NSLayoutConstraint.activate([
            scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
            scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            imageView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            imageView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            imageView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
            imageView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
            imageView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
        ])
    }

代码运行正常,但在切换到横向模式时会引发以下错误:

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-04-08 17:19:53.006762-0700 AcvMagicEye[16556:6738323] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x28119f020 UIView:0x102bac310.height == 63.2   (active)>",
    "<NSLayoutConstraint:0x2811a6bc0 UIView:0x102bac310.height == 284   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x2811a6bc0 UIView:0x102bac310.height == 284   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

尝试注释掉一些约束,但随后导致图像缩小,而不是使图像适合显示,然后允许用户滚动和缩放。

2 个答案:

答案 0 :(得分:0)

您忘记为imageView和scrollview添加translationsAutoresizingMaskIntoConstraints = false

class ViewController: UIViewController, UIScrollViewDelegate {

private let scrollView = UIScrollView(frame: .zero)
private let imageView = UIImageView(image: UIImage(named: "jupiter"))

override func viewDidLoad() {
    super.viewDidLoad()
    setup()
}

private func setup() {

    imageView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.addSubview(imageView)

    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.delegate = self
    view.addSubview(scrollView)

    NSLayoutConstraint.activate([
        scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),

        imageView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
        imageView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
        imageView.topAnchor.constraint(equalTo: scrollView.topAnchor),
        imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
        imageView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
        imageView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
    ])
}
}

答案 1 :(得分:0)

  1. 您忘记为scrollview和imageview都添加了translationsAutoresizingMaskIntoConstraints = false
  2. 您对图像视图同时使用中心X,Y和顶部,底部约束。还会导致崩溃。如果使用Center X,Y,则应使用宽度,高度约束。在这种情况下,您必须使用顶部,底部约束。

private func setup() {


    scrollView.addSubview(imageView)
    imageView.translatesAutoresizingMaskIntoConstraints = false

    view.addSubview(scrollView)
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.delegate = self

    NSLayoutConstraint.activate([
        scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),

        imageView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
        imageView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
        imageView.topAnchor.constraint(equalTo: scrollView.topAnchor),
        imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
    ])
}
}