gpt4 book ai didi

ios - 通过更新约束在底部的 MGLMapView scaleBar 位置

转载 作者:搜寻专家 更新时间:2023-10-30 20:23:49 26 4
gpt4 key购买 nike

默认情况下 MGLMapViewscaleBar View 放在 map 的左上角。我想将它移到左下角,但我在执行此操作时遇到了问题。要么是我的 NSLayoutConstraint 代码有误,要么是发生了其他事情。 scaleBar 仍然停留在左上角。

这是我尝试过的:

NSMutableArray *scaleBarConstraints = [[NSMutableArray alloc] init];
[self.mapboxMapView removeConstraints:self.mapboxMapView.scaleBar.constraints];
[scaleBarConstraints addObject:
[NSLayoutConstraint constraintWithItem:self.mapboxMapView.scaleBar
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.mapboxMapView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:8.0 + self.mapboxMapView.contentInset.bottom]];
[scaleBarConstraints addObject:
[NSLayoutConstraint constraintWithItem:self.mapboxMapView.scaleBar
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.mapboxMapView
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:8.0 + self.mapboxMapView.contentInset.left]];

[self.mapboxMapView addConstraints:scaleBarConstraints];

还有其他方法可以做到这一点还是我错过了什么?

最佳答案

首先,您的第一个约束指定了一个 NSLayoutRelationGreaterThanOrEqual 关系,所以这意味着您的 scaleBar 的底边必须在某处上方 map View 的底部边缘(+您指定的常量)。

当比例尺位于 map View 的顶部时,这显然正确。因此,请尝试将 NSLayoutRelationGreaterThanOrEqual 替换为 NSLayoutRelationEqual,看看它是否有效。

警告 1:

您正在向 self.mapboxMapView 添加约束,它不是 scaleBar 的直接父 View 。 (在 Mapbox 库中,他们将约束添加到 containerView。)这是不好的做法。我推荐使用

[NSLayoutConstraint activateConstraints:scaleBarConstraints];

相反,它会自动将约束添加到适当的 View 中。 (Apple 在 their documentation 中推荐了这一点。)

警告 2:

通常您不应该破解另一个类(您不拥有)的 View 层次结构。您不能假设它不会在某个时间点创建新的约束,这可能会覆盖您自己的约束或与您自己的约束发生冲突。

关于ios - 通过更新约束在底部的 MGLMapView scaleBar 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51864663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com