ios - UIViewController View 的框架在呈现后发生变化(viewController : animated: completion: )

标签 ios iphone swift cocoa-touch uiviewcontroller

我有一个 UIViewController,我在 ViewDidLoad 中设置了它的大小,如下所示:

//Setting view's frame
let width = UIScreen.main.bounds.size.width - 10
let height = (43 * UIScreen.main.bounds.height) / 100 //%43 of the screen
self.view.frame = CGRect(x: 5, y: 80, width: width, height: height)

它工作得很好,直到我尝试呈现另一个这样的 ViewController :

//Presenting AutoCompleteVC
self.autocompleteVC = GMSAutocompleteViewController()
self.autocompleteVC.delegate = self
self.present(self.autocompleteVC, animated: true, completion: nil)

在我关闭此 View (self.autocompleteVC.dismiss(animated: true, succession: nil)) 后,第一个 ViewController View 的框架更改为全屏的框架。

知道如何修复它吗?

谢谢!

最佳答案

viewWillLayoutSubviews中设置框架。那时框架已经已知。像这样:

public override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

   //Setting view's frame
   let width = UIScreen.main.bounds.size.width - 10
   let height = (43 * UIScreen.main.bounds.height) / 100 //%43 of the screen
   self.view.frame = CGRect(x: 5, y: 80, width: width, height: height)
}

关于ios - UIViewController View 的框架在呈现后发生变化(viewController : animated: completion: ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410463/

相关文章:

ios - 将不同的参数传递给 IBAction

iphone - iOS 在 UITableView 下面添加 UIView

ios - UIAlertController .actionSheet 项目之间的分隔线不一样

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

ios - SceneKit:像 SpriteKit 一样通过直接设置 velocity 属性来移动 SCNNode?

ios - 在 Apple Watch Storyboard 中的图像顶部添加标签

iphone - 具有时间延迟的 performSelector 在 for 循环中不起作用 - iPhone

swift - 在 nil-check 之后发现 nil

ios - 从 MKPlacemark 创建 MKMapItem 时,MKMapitem 的 Placemark.name 为 nil

iphone - 我如何知道何时释放 NSDate 对象?