MKMapView与零高度约束斗争 - 地图视图的edgeInsets?

时间:2017-04-10 12:18:53

标签: ios mkmapview nslayoutconstraint

我有一个约{300}的}高在另外两个视图之间折叠,只是像折叠视图时一样上下调整高度。

enter image description here

Main()

当视图启动时,它的高度为零..

MKMapView

有一个令人愤怒的警告......

@IBOutlet var heightMap: NSLayoutConstraint!
@IBOutlet var theMap: MKMapView!

请注意,它似乎在挣扎于“edgeInsets”或可能是“Edge Insets”(带有空格!)的顶部和底部,

override func viewDidLoad() {
    super.viewDidLoad()
    ..
    heightMap.constant = 0
}

地图视图具有只读属性[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:0x6080000904f0 MKMapView:0x7fe6b3000600.height == 0 (active)>", "<NSLayoutConstraint:0x608000093a60 UILayoutGuide:0x60800018fd80'Edge Insets'.top == MKMapView:0x7fe6b3000600.top + 8 (active)>", "<NSLayoutConstraint:0x608000093b50 UILayoutGuide:0x60800018fd80'Edge Insets'.bottom == MKMapView:0x7fe6b3000600.bottom (active)>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x608000093b50 UILayoutGuide:0x60800018fd80'Edge Insets'.bottom == MKMapView:0x7fe6b3000600.bottom (active)>

(实际上,当我打印出来时,不管它是什么,无论如何它都是零。

'Edge Insets'.top == MKMapView:0x7fe6b3000600.top + 8
'Edge Insets'.bottom == MKMapView:0x7fe6b3000600.bottom

我甚至无法在MKMapView上找到任何关于“边缘插入”的信息,这里的交易是什么?

我仔细检查了未安装的约束和常见问题。

2 个答案:

答案 0 :(得分:2)

有趣的是,如果初始hieght为零,我只会得到这个错误,所以这似乎是mapview初始设置的问题。他们的解决方案似乎是

  1. 将mapView放在故事板中(我做了100次)
  2. 请 通过检查界面构建器
  3. 中的隐藏框来隐藏mapView

    当它显示mapView的时候:

    1. 取消隐藏
    2. 最小化它(我将常量设置为.ulpOfOne而不是零,因为技术上帧不应该是退化的。)
    3. 动画到最终位置

      class ViewController: UIViewController{
          @IBOutlet weak var m: MKMapView!
          @IBOutlet weak var h: NSLayoutConstraint!
          @IBAction func t(_ sender: Any) {
              m.isHidden = false
              h.constant = .ulpOfOne
              view.layoutIfNeeded()
              h.constant = 100
              UIView.animate(withDuration: 3) {
                  self.view.layoutIfNeeded()
              }
          }
      }
      
    4. 仅供参考我实际上会使用UIStackView,而只是动画isHidden,然后你甚至不必处理代码中的约束。

答案 1 :(得分:1)

我一直在努力解决同样的问题,并且每当我想要隐藏MKMapView(手动使用高度限制或在UIStackView内)时都会收到此警告。我注意到我可以通过在激活零高度约束之前将地图视图的边界高度设置为0来阻止警告。所以我写了一个小类来封装bug:

SplashScreenActivity

这个类适用于我的项目,其中MKMapView嵌入在UIStackView中。根据您的设置,您可能必须覆盖另一种方法。为UIViewAlertForUnsatisfiableConstraints设置符号断点以查看调用堆栈。

我还提交了一个雷达,你可以在这里找到:https://openradar.appspot.com/radar?id=6109127172423680