MKMapView 在零高度约束下挣扎 - 地图视图的 edgeInsets? [英] MKMapView struggles with zero height constraint - edgeInsets of map view?

查看:46
本文介绍了MKMapView 在零高度约束下挣扎 - 地图视图的 edgeInsets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MKMapView 大约 300 高,它在两个其他视图之间折叠,只需像折叠视图时通常所做的那样对高度进行上下动画.

I have an MKMapView about 300 high which collapses between two other views, simply animating the height up and down as you'd usually do when collapsing a view.

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

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

When the view launches it is height zero..

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

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

there's an infuriating warning...

[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)>

请注意,它似乎在与edgeInsets"或可能的Edge Insets"(带空格!)顶部和底部苦苦挣扎,

Note that it seems to be struggling with "edgeInsets" or possibly "Edge Insets" (with a space!) top and bottom,

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

地图视图具有只读属性 .alignmentRectInsets

Map views have a read-only property .alignmentRectInsets

(实际上当我打印出来的时候,不管它是什么,反正都是零..

(Actually when I print that out, whatever it is, it is zero anyway..

let ari = theMap.alignmentRectInsets
print("wth \(ari)")
> wth UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

)

我什至在 MKMapView 上找不到任何关于边缘插入"的信息,这是怎么回事?

I can't even find any info about "edge insets" on a MKMapView, what's the deal here?

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

I've carefully checked for not-installed constraints and the usual problems.

推荐答案

有趣的是,如果初始高度为零,我只会收到此错误,因此这似乎是地图视图初始设置的问题.他们的解决方案似乎是

Interestingly I only get this error if the initial hieght is zero, so it seems to be an issue with the initial setup of the mapview. The solution them seems to be to be

  1. 在故事板中给 mapView 一个高度(我做了 100 个)
  2. 制作通过检查界面构建器中的隐藏框隐藏的mapView

何时显示地图视图:

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

  1. Unhide it
  2. Minimize it (I set the constant to .ulpOfOne instead of zero because technically frames are not supposed to be degenerate.)
  3. Animate to the final position

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()
        }
    }
}

仅供参考,我实际上会为此使用 UIStackView,而只是为 isHidden 设置动画,然后您甚至不必处理代码中的约束.

FYI I would actually use a UIStackView for this and just animate isHidden instead and then you don't even have to deal with the constraint in code.

这篇关于MKMapView 在零高度约束下挣扎 - 地图视图的 edgeInsets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆