Swift 4.2 SafeAreaLayoutGuide frame is the same as

2019-08-31 04:52发布

问题:

I'm using safearealayout guides, programmatically (i.e.) without storyboards. I want to access the safeAreaLayoutGuides, layoutFrame size within my app but don't know when to call it. If you call on viewDidLoad, the size returned is the same as the full view.frame size. When does the app catch up and readjust? My full app has 5 screens within a menubarcontroller. The first screen loads with content constrained to view.topAnchor when I've activated view.safeAreaLayoutGuide.topAnchor. If you switch to another tab/screen, that screen is okay and when you return to the first the layout has corrected itself. In a basic example, I added a button which prints the frame sizes after the view has loaded and it returns the correct size. I don't want my user to have to press a button to load the view correctly.

class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        print(view.frame) // Returns (0.0, 0.0, 375.0, 812.0)
        print(view.safeAreaLayoutGuide.layoutFrame) // Returns (0.0, 0.0, 375.0, 812.0)

    }

    @IBAction func button(_ sender: Any) {

        print(view.frame) // Returns (0.0, 0.0, 375.0, 812.0)
        print(view.safeAreaLayoutGuide.layoutFrame) // Returns (0.0, 44.0, 375.0, 734.0)

    }
}

回答1:

Thanks to Milan. I have experimented and found the following:

`

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    print("ViewWillLayout: \(view.safeAreaLayoutGuide.layoutFrame)")
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    print("ViewDidAppear \(view.safeAreaLayoutGuide.layoutFrame)")
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    print("ViewDidLayoutSubviews \(view.safeAreaLayoutGuide.layoutFrame)")
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    print("ViewWillAppear \(view.safeAreaLayoutGuide.layoutFrame)")
}`

Results from console

  • View.Frame size: (0.0, 0.0, 375.0, 812.0) // Frame size
  • ViewDidLoad: (0.0, 0.0, 375.0, 812.0) // Same as frame size
  • ViewWillAppear (0.0, 0.0, 375.0, 812.0) // Same as frame size
  • ViewWillLayout: (0.0, 88.0, 375.0, 690.0) // Works
  • ViewDidLayoutSubviews (0.0, 88.0, 375.0, 690.0) // Works
  • ViewDidAppear (0.0, 88.0, 375.0, 690.0) // Works