Could not inset legal attribution from corner 4 swift

后端 未结 5 1138
花落未央
花落未央 2021-02-13 13:07

I am new to Xcode and mobile app. I am doing an app to find the current location. I tested it on the simulator and got this message in the console. \"Could not inset legal attri

5条回答
  •  忘了有多久
    2021-02-13 13:15

    I believe the legal inset bug is in Apple's mapkit initialization code and unrelated to what we do when we use the code. Here's my reasoning.

    If you use storyboards and the UIMapKit drag and drop module, the legal inset error pops up somewhere between the call to the initial call to application in the app delegate and the first viewDidLoad call, i.e., it's an OS error.

    I got curious if I could work around it and wrote a version that doesn't use the storyboard editor. I was thinking that perhaps the editor was inserting some broken code into the app. My test app has two views, the start view and a second view with the map on it. The code is shown below. Don't be harsh, it's my first attempt at understanding what's going on with controllers views and subviews using programmatic views and subviews. It isn't pretty but it works to isolate the legal inset bug.

    I started by building a new xcode project and deleting the storyboard. I then replaced the application function stub in appdelegate with the following code:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions 
     launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let viewController = ViewController()
        self.window?.rootViewController = viewController
        self.window?.makeKeyAndVisible()
        let mapView = mapViewController()
        mapView.otherView = viewController
        viewController.mapView = mapView
        return true
    }
    

    Even though the mapView is created here, the legal inset error doesn't show up. What does show up are a couple of other messages about scale and compass having some sort of issue. Since I was focusing on the legal inset bug, I ignored them and moved on.

    I then replaced the default view controller with code that created two subviews, one a subview that serves as a button to switch to the second view controller and the second subview that serves as "you're on the first view" marker view. That required figuring out how to handle a tap. The color changing code in the tap handler was initial "hello world. I see a tap" code and serves no other purpose.

    import UIKit
    import MapKit
    import CoreLocation
    
    class ViewController: UIViewController,CLLocationManagerDelegate {
    
    let DynamicView = UIView()
    let switchView = UIView(frame: CGRect(x:  UIScreen.main.bounds.minX+10, y:  UIScreen.main.bounds.minY+20, width: 40, height: 40))
    public var mapView:mapViewController? = nil
    public var otherView:UIViewController? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = .white
    
        DynamicView.backgroundColor = .yellow
        DynamicView.layer.cornerRadius = 25
        DynamicView.layer.borderWidth = 2
    
        switchView.backgroundColor = .orange
        switchView.layer.borderWidth = 2
    
        var gR = UITapGestureRecognizer(target: self, action:#selector(self.handlebigTap(_:)))
        DynamicView.addGestureRecognizer(gR)
        gR = UITapGestureRecognizer(target: self, action:#selector(self.handlesmallTap(_:)))
        switchView.addGestureRecognizer(gR)
        self.view.addSubview(switchView)
        self.view.addSubview(DynamicView)
    
        DynamicView.translatesAutoresizingMaskIntoConstraints = false
        let margins = view.layoutMarginsGuide
        DynamicView.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 10).isActive = true
        DynamicView.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 10).isActive = true
        DynamicView.topAnchor.constraint(equalTo: margins.topAnchor, constant: 40).isActive = true
        DynamicView.bottomAnchor.constraint(equalTo: margins.topAnchor, constant: 110).isActive = true
    
    }
    @objc func handlebigTap(_ sender: UITapGestureRecognizer) {
        if ( self.DynamicView.backgroundColor == .green){
            self.DynamicView.backgroundColor = .blue
        } else {
            self.DynamicView.backgroundColor = .green
        }
    }
    @objc func handlesmallTap(_ sender: UITapGestureRecognizer) {
        if ( self.switchView.backgroundColor == .orange){
            self.switchView.backgroundColor = .blue
            present(mapView!, animated: true, completion: nil)
        } else {
            self.switchView.backgroundColor = .orange
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    }

    At this point, I checked to see if the first call to viewDidLoad triggered the legal inset error and saw that it did not. That meant the error was being triggered somewhere in the mapKit initialization code which was yet to be built. I simply copied and pasted the first viewController into a new file and called that mapViewController. I commented out the DynamicView code left over from the first controller and added the mapKit initialization code as shown here:

    import UIKit
    import MapKit
    import CoreLocation
    
    class mapViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {
    
    
    let DynamicView = UIView(frame: CGRect(x:  UIScreen.main.bounds.maxX-110, y: UIScreen.main.bounds.maxY-110, width: 100, height: 100))
    let switchView = UIView(frame: CGRect(x:  UIScreen.main.bounds.minX+10, y:  UIScreen.main.bounds.minY+20, width: 40, height: 40))
    var mapView = MKMapView()
    public var otherView:UIViewController? = nil
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.view.backgroundColor = .white
    
     // DynamicView.backgroundColor = .yellow
     // DynamicView.layer.cornerRadius = 25
     // DynamicView.layer.borderWidth = 2
    
        switchView.backgroundColor = .orange
        switchView.layer.borderWidth = 2
    
        var gR = UITapGestureRecognizer(target: self, action:#selector(self.handlebigTap(_:)))
        DynamicView.addGestureRecognizer(gR)
         gR = UITapGestureRecognizer(target: self, action:#selector(self.handlesmallTap(_:)))
        switchView.addGestureRecognizer(gR)
        self.view.addSubview(switchView)
     // self.view.addSubview(DynamicView)
    
    
     // mapView.frame = CGRect(x: 10, y: 60, width: view.frame.size.width-20, height: 300)
    
        mapView.mapType = MKMapType.standard
        mapView.isZoomEnabled = true
        mapView.isScrollEnabled = true
    
        view.addSubview(mapView)
        mapView.translatesAutoresizingMaskIntoConstraints = false
        let margins = view.layoutMarginsGuide
        mapView.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 10).isActive = true
        mapView.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 0).isActive = true
        mapView.topAnchor.constraint(equalTo: margins.topAnchor, constant: 45).isActive = true
        mapView.bottomAnchor.constraint(equalTo: margins.bottomAnchor, constant: -20).isActive = true
    
    
    }
    @objc func handlebigTap(_ sender: UITapGestureRecognizer) {
        if ( self.DynamicView.backgroundColor == .green){
            self.DynamicView.backgroundColor = .blue
        } else {
            self.DynamicView.backgroundColor = .green
        }
    }
    @objc func handlesmallTap(_ sender: UITapGestureRecognizer) {
        if ( self.switchView.backgroundColor == .orange){
            self.switchView.backgroundColor = .blue
             dismiss(animated: true, completion: nil)
        } else {
            self.switchView.backgroundColor = .orange
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
    
    
    
    }
    

    }

    I ran the above code and stepped through looking for the legal inset message to show up. This is the offending line:

    mapView.frame = CGRect(x: 10, y: 60, width: view.frame.size.width-20, height: 300)

    As soon as the view is framed, the error message comes out. Doesn't matter what dimensions you give the view, the message appears. OK.... Maybe the message expects a constraint based framing instead of hard coded per the offending line.

    I commented out the line and added constraints to the view and still the error popped up.

    At this point I gave up. I couldn't figure out how to configure a map view so the error message doesn't show up. Hopefully Apple will pipe up and say something at this point or someone else will pick up the baton to see if there's a way to configure mapkit to stop spewing error messages.

    As for myself at least I learned how to dynamically add views, subviews, gesture recognizers and constraints so my time was profitably spent chasing the bug. Thanks to all who posted sample dynamic view code, gesture recognition code, subview code and constraint code. You may recognize a bit of your code here.

提交回复
热议问题