【问题标题】:How to correct the center of a view after extending it under navigation bar在导航栏下扩展视图后如何更正视图的中心
【发布时间】:2016-04-30 10:56:14
【问题描述】:

在这个视图控制器中,我有一个导航栏、一个 mkmapview 和一个集合视图。我有以下代码:

import UIKit
import MapKit

class PhotoAlbumViewController: UIViewController {
    var annotation: VTAnnotation!
    var span: MKCoordinateSpan!

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        navigationController?.navigationBarHidden = false
        mapView.clipsToBounds = false
        mapView.addAnnotation(annotation)
        mapView.userInteractionEnabled = false
        let region = MKCoordinateRegion(center: annotation.coordinate, span: span)
        mapView.setRegion(region, animated: false)

    }
}

如果我的 MKMapView 设置为位于导航栏和 Storyboard 中的集合视图之间,则图钉将根据需要居中。但我没有显示导航栏的半透明性。

如果我的 MKMapView 设置为位于导航栏下方,我可以显示导航栏的半透明,但图钉现在偏离中心。

如何使图钉显示在可见区域的中心,并显示导航栏的半透明性?

我觉得这应该在文档中。但我一直在寻找没有运气,也许我不知道要搜索的关键字。

【问题讨论】:

  • mapview 被布局后,你应该认为它是新的可见部分。尝试在“viewDidLayoutSubviews”之后更改地图区域。
  • 在 mapkit 中,有一些方法可以计算视图中某个点的坐标,反之亦然 - 这是通过它的方法。不幸的是,我现在没有机会玩代码。如果你没有把你的注释放到正确的地方 - 戳我回来 - 我们会尝试移动它:)

标签: ios swift uiview storyboard swift2


【解决方案1】:

我一直在努力解决同样的问题,并设法通过使用setVisibleMapRect(_:edgePadding:animated:) 方法向MKMapView 添加一些顶部填充来解决它:

    let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
    let region = MKCoordinateRegion(center: annotation.coordinate, span: span)

    // 1. Set the Region
    mapView.setRegion(region, animated: false)

    // 2. Store the currently visible area
    let currentMapRect = mapView.visibleMapRect

    // 3. Store the sum of the navigation bar height and the top safe area inset
    var topPadding: CGFloat = 0
    if let safeAreaTopInset = UIApplication.shared.keyWindow?.safeAreaInsets.top,
        let navigationBarHeight = navigationController?.navigationBar.frame.height {
        topPadding = safeAreaTopInset + navigationBarHeight
    }

    // 4. Add the top padding to the map's visible area
    let padding = UIEdgeInsets(top: topPadding, left: 0.0, bottom: 0.0, right: 0.0)
    mapView.setVisibleMapRect(currentMapRect, edgePadding: padding, animated: true)

    mapView.addAnnotation(annotation)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多