ios - MKMapView 与零高度约束作斗争 - map View 的 edgeInsets?
<p><p>我有一个大约 300 高的 <code>MKMapView</code>,它在其他两个 View 之间折叠,就像折叠 View 时通常所做的那样简单地设置高度的动画。</p>
<p> <a href="/image/xKOYH.png" rel="noreferrer noopener nofollow"><img src="/image/xKOYH.png" alt="enter image description here"/></a> </p>
<pre><code>@IBOutlet var heightMap: NSLayoutConstraint!
@IBOutlet var theMap: MKMapView!
</code></pre>
<p>当 View 启动时,它的高度为零..</p>
<pre><code>override func viewDidLoad() {
super.viewDidLoad()
..
heightMap.constant = 0
}
</code></pre>
<p>有一个令人愤怒的警告......</p>
<pre><code> 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)>
</code></pre>
<p>请注意,它似乎在与“edgeInsets”或可能的“Edge Insets”(带空格!)上下挣扎,</p>
<pre><code>'Edge Insets'.top == MKMapView:0x7fe6b3000600.top + 8
'Edge Insets'.bottom == MKMapView:0x7fe6b3000600.bottom
</code></pre>
<p> mapView 有一个只读属性<code>.alignmentRectInsets</code></p>
<p>(其实当我把它打印出来的时候,不管它是什么,反正它是零..</p>
<pre><code>let ari = theMap.alignmentRectInsets
print("wth \(ari)")
> wth UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
</code></pre>
<p>)</p>
<p>我什至在 MKMapView 上找不到任何关于“边缘插图”的信息,这是怎么回事?</p>
<p>我已经仔细检查了未安装的约束和常见问题。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>有趣的是,如果初始高度为零,我只会收到此错误,因此这似乎是 mapView 初始设置的问题。他们的解决方案似乎是</p>
<ol>
<li>在 Storyboard 中给 mapView 一个高度(我做了 100 个)</li>
<li>制作
通过检查界面生成器中的隐藏框来隐藏mapView</li>
</ol>
<p>何时显示 mapView:</p>
<ol>
<li>取消隐藏</li>
<li>将其最小化(我将常量设置为 .ulpOfOne 而不是零,因为从技术上讲,帧不应该退化。)</li>
<li><p>动画到最终位置</p>
<pre><code>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()
}
}
}
</code></pre> </li>
</ol>
<p>仅供引用,我实际上会为此使用 UIStackView 并只是为 isHidden 设置动画,然后您甚至不必处理代码中的约束。</p></p>
<p style="font-size: 20px;">关于ios - MKMapView 与零高度约束作斗争 -mapView 的 edgeInsets?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43322973/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43322973/
</a>
</p>
页:
[1]