Method swizzling in Swift

后端 未结 5 1662
深忆病人
深忆病人 2020-12-24 09:38

There is a little problem with UINavigationBar which I\'m trying to overcome. When you hide the status bar using prefersStatusBarHidden() method in

5条回答
  •  孤城傲影
    2020-12-24 09:59

    While this isn't a direct answer to your Swift question (seems like it may be a bug that load doesn't work for Swift extensions of NSObjects), however, I do have a solution to your original problem

    I figured out that subclassing UINavigationBar and providing a similar implementation to the swizzled solution works in iOS 7 and 8:

    class MyNavigationBar: UINavigationBar {
        override func sizeThatFits(size: CGSize) -> CGSize {
            if UIApplication.sharedApplication().statusBarHidden {
                return CGSize(width: frame.size.width, height: 64)
            } else {
                return super.sizeThatFits(size)
            }
        }
    }
    

    Then update the class of the Navigation Bar in your storyboard, or use init(navigationBarClass: AnyClass!, toolbarClass: AnyClass!) when constructing your navigation controller to use the new class.

提交回复
热议问题