Method swizzling in Swift

后端 未结 5 1625
深忆病人
深忆病人 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 10:07

    The first problem I faced was Swift extension can't have stored properties.

    First of all, categories in Objective-C also cannot have stored properties (called synthesized properties). The Objective-C code you linked to uses a computed property (it provides explicit getters and setters).

    Anyway, getting back to your problem, you cannot assign to your property because you defined it as a read-only property. To define a read-write computed property, you would need to provide a getter and setter like this:

    var fixedHeightWhenStatusBarHidden: Bool {
        get {
            return objc_getAssociatedObject(self, "FixedNavigationBarSize").boolValue
        }
        set(newValue) {
            objc_setAssociatedObject(self, "FixedNavigationBarSize", NSNumber(bool: newValue), UInt(OBJC_ASSOCIATION_RETAIN))
        }
    }
    

提交回复
热议问题