Ios Xcode UIStackView奇怪的布局约束问题

Ios Xcode UIStackView奇怪的布局约束问题,ios,xcode,autolayout,uikit,uistackview,Ios,Xcode,Autolayout,Uikit,Uistackview,我正在空UIScrollView中的空UIStackView中添加MapView(主要是UIView)。我通过以下方式实现了它: private func setupScrollView() { scrollView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(scrollView) let frameLayoutGuide = scr

我正在空UIScrollView中的空UIStackView中添加MapView(主要是UIView)。我通过以下方式实现了它:

    private func setupScrollView() {
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(scrollView)
        
        let frameLayoutGuide = scrollView.frameLayoutGuide
        
        NSLayoutConstraint.activate([
            frameLayoutGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            frameLayoutGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            frameLayoutGuide.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            frameLayoutGuide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
        ])
    }

    private func setupMainStackView() {
        mainStackView.axis = .vertical
        mainStackView.translatesAutoresizingMaskIntoConstraints = false
        
        scrollView.addSubview(mainStackView)
        
        let contentLayoutGuide = scrollView.contentLayoutGuide
        
        NSLayoutConstraint.activate([
            mainStackView.widthAnchor.constraint(equalTo: view.widthAnchor),
            mainStackView.leadingAnchor.constraint(equalTo: contentLayoutGuide.leadingAnchor),
            mainStackView.trailingAnchor.constraint(equalTo: contentLayoutGuide.trailingAnchor),
            mainStackView.topAnchor.constraint(equalTo: contentLayoutGuide.topAnchor),
            mainStackView.bottomAnchor.constraint(equalTo: contentLayoutGuide.bottomAnchor)
        ])
        
        setupMapView()
    }

    private func setupMapView() {
        mapView.translatesAutoresizingMaskIntoConstraints = false
        mapView.isUserInteractionEnabled = false
        mainStackView.addArrangedSubview(mapView)
        
        NSLayoutConstraint.activate([
            // whenever the constant is set to a value other then zero, constraint issues pop up in the console
            mapView.widthAnchor.constraint(equalTo: mainStackView.widthAnchor, constant: -10), 
            mapView.centerXAnchor.constraint(equalTo: mainStackView.centerXAnchor),
            mapView.heightAnchor.constraint(equalTo: mapView.widthAnchor, multiplier: 0.618)
        ])
    }
但是,只要将
mapView
widthAnchor
约束的常数设置为零以外的任何值,控制台中就会弹出约束问题。我这样做是因为我希望在
地图视图的左边缘和右边缘都添加填充

[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:0x282b37070 MKMapView:0x139835800.width == UIStackView:0x138a13480.width - 42.8   (active)>",
    "<NSLayoutConstraint:0x282b372a0 MKMapView:0x139835800.centerX == UIStackView:0x138a13480.centerX   (active)>",
    "<NSLayoutConstraint:0x282b1c190 'UISV-canvas-connection' H:[MKMapView:0x139835800]-(0)-|   (active, names: '|':UIStackView:0x138a13480 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x282b372a0 MKMapView:0x139835800.centerX == UIStackView:0x138a13480.centerX   (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.
2020-12-26 19:01:44.048851-0800 LiQing Watercolor[16247:4990949] [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:0x282b37070 MKMapView:0x139835800.width == UIStackView:0x138a13480.width - 42.8   (active)>",
    "<NSLayoutConstraint:0x282b1c140 'UISV-canvas-connection' UIStackView:0x138a13480.leading == MKMapView:0x139835800.leading   (active)>",
    "<NSLayoutConstraint:0x282b1c190 'UISV-canvas-connection' H:[MKMapView:0x139835800]-(0)-|   (active, names: '|':UIStackView:0x138a13480 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x282b1c140 'UISV-canvas-connection' UIStackView:0x138a13480.leading == MKMapView:0x139835800.leading   (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.
[LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2020-12-26 19:01:44.048851-0800李青水彩[16247:4990949][布局约束]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
我还发现,当使用
mainStackView.addSubview(mapView)
而不是
mainStackView.addArrangedSubview(mapView)
添加地图视图时,我的代码工作得非常好


如果有人知道如何解决这个问题,那就太好了!谢谢。

你做错了。。。首先,您不应该在滚动视图的
.frameLayoutGuide
上设置约束——您可以设置与框架布局指南相关的约束。通读这个答案-我认为它会解决你的问题:你做的事情非常错误。。。首先,您不应该在滚动视图的
.frameLayoutGuide
上设置约束——您可以设置与框架布局指南相关的约束。通读此答案-我认为它将解决您的问题: