Subview没有使用容器的完整视图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Subview没有使用容器的完整视图相关的知识,希望对你有一定的参考价值。

所以,我使用UIView作为子视图的容器,将子视图保存在其中。在实现之后,尽管仍然有一些容器可以看到,因为子视图没有像它应该那样使用完整的容器。我做错什么了吗?我错过了一些代码吗?这就是它目前的样子...... 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 
}   

您还需要在self.googleMapsView.clipsToBounds = true中删除此行viewDidLoad

以上是关于Subview没有使用容器的完整视图的主要内容,如果未能解决你的问题,请参考以下文章

使用 IOS 中的约束将 UIView 中的子视图居中

通过 subView 禁用触摸视图

iOS 7 - UIScrollView subView 不显示

动画 MainView 没有 SubView iOS

UIStackView:从xib加载视图和更新subView的高度约束没有反映任何变化?

在Subview位置选择滚动视图子视图滚动