ios - subview 未使用容器的完整 View

标签 ios swift xcode uiview subviews

因此,我使用 UIView 作为 subview 的容器,该 subview 将在其中保存 Google map 。实现后,由于 subview 没有像应有的那样使用完整的容器,因此仍然可以看到一些容器。我做错什么了吗?我缺少一些代码吗? 这就是它目前的样子... https://imgur.com/a/k0pj0

class SecondViewController: UIViewController, 
CLLocationManagerDelegate, LocateOnTheMap, 
GMSAutocompleteFetcherDelegate, UISearchBarDelegate {



func didAutocomplete(with predictions: [GMSAutocompletePrediction]) {
for prediction in predictions {

    if let prediction = prediction as GMSAutocompletePrediction?{
        self.resultsArray.append(prediction.attributedFullText.string)
    }
}
self.searchResultController.reloadDataWithArray(self.resultsArray)
print(resultsArray)
 }

 func didFailAutocompleteWithError(_ error: Error) {

 }


@IBOutlet weak var MapViewContainer: UIView!

var googleMapsView: GMSMapView!
var searchResultController: SearchResultsController!
var resultsArray = [String]()
var gmsFetcher: GMSAutocompleteFetcher!

 @IBAction func searchWithAddress(_ sender: AnyObject) {
 let searchController = UISearchController(searchResultsController: searchResultController)
searchController.searchBar.delegate = self
self.present(searchController, animated:true, completion: nil)


 }


 let locationDelegate = LocationDelegate()
 var latestLocation: CLLocation? = nil
 var yourLocationBearing: CGFloat { return 
 latestLocation?.bearingToLocationRadian(self.yourLocation) ?? 0 }
 var yourLocation: CLLocation {
get { return UserDefaults.standard.currentLocation }
set { UserDefaults.standard.currentLocation = newValue }
}

//Location Manager Variable

let manager = CLLocationManager()

 //viewDidLoad

override func viewDidLoad() {
super.viewDidLoad()

//Map Options

self.googleMapsView = GMSMapView(frame: self.MapViewContainer.bounds)
self.googleMapsView.clipsToBounds = true
self.MapViewContainer.addSubview(self.googleMapsView)

searchResultController = SearchResultsController()
searchResultController.delegate = self
gmsFetcher = GMSAutocompleteFetcher()
gmsFetcher.delegate = self

googleMapsView.isMyLocationEnabled = true
googleMapsView.mapType = .normal
googleMapsView.settings.compassButton = true
googleMapsView.settings.myLocationButton = true
googleMapsView.settings.zoomGestures = true

}

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

 let location = locations.last

 let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude:(location?.coordinate.longitude)!, zoom:14)
googleMapsView.animate(to: camera)

//Finally stop updating location otherwise it will come again and again in this delegate
self.locationManager.stopUpdatingLocation()

}

func locateWithLongitude(_ lon: Double, andLatitude lat: Double, andTitle title: String) {

 DispatchQueue.main.async { () -> Void in

    let position = CLLocationCoordinate2DMake(lat, lon)
    let marker = GMSMarker(position: position)

    let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 10)
    self.googleMapsView.camera = camera

    marker.title = "Address : \(title)"
    marker.map = self.googleMapsView


}

}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {


self.resultsArray.removeAll()
gmsFetcher?.sourceTextHasChanged(searchText)


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}
}

最佳答案

您需要重写 ViewController 的 viewDidLayoutSubviews,正如我在评论中所说的,

func viewDidLayoutSubviews() { 
  super.viewDidLayoutSubviews() 
  self.googleMapsView.frame = self.MapViewContainer.bounds 
}   

您还需要删除 viewDidLoad 中的这一行 self.googleMapsView.clipsToBounds = true

关于ios - subview 未使用容器的完整 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49079284/

相关文章:

ios - Apple M1 芯片上的 Firebase 分析和其他模块

ios - 使用 Apple Touch ID 登录外部数据库

ios - 带有文本长度计数器的 UITextField

ios - 如何正确使用 NSTimer 来创建自动幻灯片放映

ios - 在一个 tableView 上同时获取 2 个 URLRequests

ios - Google Maps iOS SDK 中的位置点显示为正方形

ios - 核心数据和 iCloud : exception when NSPersistentStoreCoordinatorStoresWillChangeNotification received

ios - 使用结构在 Swift 5 中使用 JSONDecoder 解析 JSON

ios - 在 7.3/9/2+ Swift 中,设备旋转时如何禁用旋转动画?

iphone - ios : set cell image dimension