Method swizzling in Swift

后端 未结 5 1660
深忆病人
深忆病人 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:53

    Objective-C, which uses dynamic dispatch supports the following:

    Class method swizzling:

    The kind you're using above. All instances of a class will have their method replaced with the new implementation. The new implementation can optionally wrap the old.

    Isa-pointer swizzling:

    An instance of a class is set to a new run-time generated sub-class. This instance's methods will be replaced with a new method, which can optionally wrap the existing method.

    Message forwarding:

    A class acts as a proxy to another class, by performing some work, before forwarding the message to another handler.

    These are all variations on the powerful intercept pattern, which many of Cocoa's best features rely on.

    Enter Swift:

    Swift continues the tradition of ARC, in that the compiler will do powerful optimizations on your behalf. It will attempt to inline your methods or use static dispatch or vtable dispatch. While faster, these all prevent method interception (in the absence of a virtual machine). However you can indicate to Swift that you'd like dynamic binding (and therefore method interception) by complying with the following:

    • By extending NSObject or using the @objc directive.
    • By adding the dynamic attribute to a function, eg public dynamic func foobar() -> AnyObject

    In the example you provide above, these requirements are being met. Your class is derived transitively from NSObject via UIView and UIResponder, however there is something odd about that category:

    • The category is overriding the load method, which will normally be called once for a class. Doing this from a category probably isn't wise, and I'm guessing that while it might have worked before, in the case of Swift it doesn't.

    Try instead to move the Swizzling code into your AppDelegate's did finish launching:

    //Put this instead in AppDelegate
    method_exchangeImplementations(
        class_getInstanceMethod(UINavigationBar.self, "sizeThatFits:"), 
        class_getInstanceMethod(UINavigationBar.self, "sizeThatFits_FixedHeightWhenStatusBarHidden:"))  
    

提交回复
热议问题