在实例化ViewController w/xib时,帧大小不正确

Kyl*_*cot 7 iphone xib ios swift

我有一个视图控制器,看起来像:

class SpotViewController: UIViewController {
  let spot: Spot

  init(spot: Spot) {
    self.spot = spot
    super.init(nibName: "SpotViewController", bundle: nil)
  }

  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    print("viewWillAppear: \(view.frame)") # => (0.0, 0.0, 600.0, 600.0)
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    print("viewDidLoad: \(view.frame)") # => (0.0, 0.0, 600.0, 600.0)
  }
}
Run Code Online (Sandbox Code Playgroud)

当我实例经由视图控制器let spotViewController = SpotViewController(spot: spot)并将其推入导航控制器将所得帧是在不正确都viewDidLoadviewWillAppear.它给了我(0.0, 0.0, 600.0, 600.0)接口构建器中的大小.

为什么会发生这种情况以及使用xib实例化视图控制器以确保帧正确的正确方法是什么?

mie*_*tus 0

这将解决您的问题,从 xib 加载 UIViewController,在 viewDidLoad 期间保持 xib 大小:

extension UIViewController {
    public override class func initialize() {
        struct Static {
            static var token: dispatch_once_t = 0
        }
        if self !== UIViewController.self {
            return
        }
        dispatch_once(&Static.token) {
            pb_applyFixToViewFrameWhenLoadingFromNib()
        }
    }

    @objc func pb_setView(view: UIView!) {
        // View is loaded from xib file
        if nibBundle != nil && storyboard == nil {
            view.frame = UIScreen.mainScreen().bounds
            view.layoutIfNeeded()
        }
        pb_setView(view)
    }

    private class func pb_applyFixToViewFrameWhenLoadingFromNib() {
        UIViewController.swizzleMethodSelector(NSSelectorFromString("setView:"), withSelector: #selector(UIViewController.pb_setView(_:)))
        UICollectionView.swizzleMethodSelector(#selector(UICollectionView.layoutSubviews), withSelector: #selector(UICollectionView.pb_layoutSubviews))
        UITableView.swizzleMethodSelector(#selector(UITableView.layoutSubviews), withSelector: #selector(UITableView.pb_layoutSubviews))
    }
}

extension UITableView {
    @objc private func pb_layoutSubviews() {
        if dataSource == nil {
            super.layoutSubviews()
        } else {
            pb_layoutSubviews()
        }
    }
}

extension UICollectionView {
    @objc private func pb_layoutSubviews() {
        if dataSource == nil {
            super.layoutSubviews()
        } else {
            pb_layoutSubviews()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)