首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MKMapView的比例未显示

MKMapView的比例未显示
EN

Stack Overflow用户
提问于 2017-11-13 20:05:40
回答 3查看 7K关注 0票数 5

我正在做一个iOS应用程序。在Xcode 9.1中,我通过

代码语言:javascript
运行
复制
let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))
mapView.isUserInteractionEnabled = false
mapView.mapType = .satellite
mapView.showsCompass = false
mapView.showsScale = true
view.addSubview(mapView)

但是,当我在模拟器中运行它时,没有显示标度,我在日志中得到了三条消息:

无法从边缘嵌入指南针9 无法从边缘9嵌入刻度 无法从拐角4嵌入法律归属

指南针没有显示(如预期的那样),但如果我将mapView.showsCompass更改为true,它也不会显示。然而,法律联系是显示出来的。我在这里错过了什么?我猜这是关于iOS 11引入的新的安全区域,但我不知道这对我想要覆盖整个屏幕的视图来说有多重要。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-11-13 23:45:21

在iOS 10或更低的

正如@Paulw11 11所说,只有在默认情况下缩放时才会显示比例。

In iOS 11

您可以使用scaleVisibilityhttps://developer.apple.com/documentation/mapkit/mkscaleview/2890254-scalevisibility

代码语言:javascript
运行
复制
let scale = MKScaleView(mapView: mapView)
scale.scaleVisibility = .visible // always visible
view.addSubview(scale)
票数 12
EN

Stack Overflow用户

发布于 2017-12-16 18:48:44

今天的天平也有同样的问题。我要一直都能看到那个天平。我花了好几个小时来解决这个问题。所以我在这里添加代码,以防万一,有人遇到了同样的问题。

有一些提示:

来自这个线程:以编程方式使用安全区布局

这个网站是:带布局锚的无痛苦约束

快乐的编码..。

哈代

代码语言:javascript
运行
复制
// "self.MapOnScreen" refers to the map currently displayed

// check if we have to deal with the scale
if #available(iOS 11.0, *) {

    // as we will change the UI, ensure it's on main thread
    DispatchQueue.main.async(execute: {

        // switch OFF the standard scale (otherwise both will be visible when zoom in/out)
        self.MapOnScreen.showsScale = false

        // build the view
        let scale = MKScaleView(mapView: self.MapOnScreen)

        // we want to use autolayout
        scale.translatesAutoresizingMaskIntoConstraints = false

        // scale should be visible all the time
        scale.scaleVisibility = .visible // always visible

        // add it to the map
        self.MapOnScreen.addSubview(scale)

        // get the current safe area of the map
        let guide = self.MapOnScreen.safeAreaLayoutGuide

        // Activate this array of constraints, which at the time removes leftovers if any
        NSLayoutConstraint.activate(
            [
                // LEFT (I do not want a change if right-to-left language) margin with an offset to safe area
                // alternative would be ".leadingAnchor", which switches to the right margin, if right-to-left language is used        
                scale.leftAnchor.constraint(equalTo: guide.leftAnchor, constant: 16.0),

                // right edge will be the middle of the map
                scale.rightAnchor.constraint(equalTo: guide.centerXAnchor),

                // top margin is the top safe area
                scale.topAnchor.constraint(equalTo: guide.topAnchor),

                // view will be 20 points high
                scale.heightAnchor.constraint(equalToConstant: 20.0)
            ]
        )
    })
}
票数 6
EN

Stack Overflow用户

发布于 2022-02-10 13:01:51

目标c相当于:-

代码语言:javascript
运行
复制
if (@available(iOS 11.0, *)) {
        // switch OFF the standard scale (otherwise both will be visible when zoom in/out)
    self.map.showsScale = false;

        // build the view

    MKScaleView* scale = [MKScaleView scaleViewWithMapView:self.map];

        // we want to use autolayout
    scale.translatesAutoresizingMaskIntoConstraints = false;

        // scale should be visible all the time
    scale.scaleVisibility = MKFeatureVisibilityVisible;// always visible
    
    // add it to the map
    [self.view addSubview:scale];
   
        // get the current safe area of the map
    UILayoutGuide * guide = self.view.safeAreaLayoutGuide;

        // Activate this array of constraints, which at the time removes leftovers if any
        [NSLayoutConstraint activateConstraints:
            @[
                // LEFT (I do not want a change if right-to-left language) margin with an offset to safe area
                // alternative would be ".leadingAnchor", which switches to the right margin, if right-to-left language is used
               //[scale.leftAnchor constraintEqualToAnchor: guide.centerXAnchor constant: -(scale.frame.size.width/2.0)],
          
                // right edge will be the middle of the map
                [scale.rightAnchor constraintEqualToAnchor: guide.centerXAnchor constant: (scale.frame.size.width/2.0)],

                // top margin is the top safe area
                [scale.bottomAnchor constraintEqualToAnchor: guide.bottomAnchor constant:-self.toolBar.frame.size.height],

                // view will be 20 points high
                [scale.heightAnchor constraintEqualToConstant: 50.0]
            ]
        ];
    
    [self.view bringSubviewToFront:scale];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47272663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档