UIAppearance setTranslucent error: Illegal property type, c for appearance setter, _installAppearanceSwizzleForSetter

大兔子大兔子 提交于 2019-12-18 18:54:13

问题


Trying out iOS 7 did:

[[UINavigationBar appearance] setTranslucent:NO];

Got crash and error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzleForSetter:'
*** First throw call stack:
(0x16ad9b8 0x142e8b6 0x16ad7ab 0x72163d 0x724c34 0x169daca 0x169d8de 0x6c09 0x228ea9 0x2296e9 0x22ab5e 0x240a6c 0x240fd9 0x22c7d5 0x35a4906 0x35a4411 0x16293e5 0x162911b 0x1653b30 0x165310d 0x1652f3b 0x22a2b1 0x22c4eb 0x6f3d 0x1d0d725)
libc++abi.dylib: terminating with uncaught exception of type NSException

Other calls work fine: eg.

[[UINavigationBar appearance] setBarStyle: UIBarStyleBlack];

When I set the translucency locally, it does not crash:

[self.navigationController.navigationBar setTranslucent:NO];

I'm assuming this is specific to iOS 7, but haven't tried out same thing in iOS 6 yet.


回答1:


[[UINavigationBar appearance] setTranslucent:NO] 

It is not available iOS 6.It is only available in iOS 7 onward.




回答2:


Here's code to work around it. The problem is that UIAppearance won't work on BOOL types. This should not be grounds for app rejection, as it uses standard (albeit hacky) procedures. Have fun.

@protocol _UITranslucentThingHack
@property (nonatomic) BOOL translucent;
@end

@interface UIView (_UITranslucentAppearanceProxyHack)
@property (nonatomic) NSNumber * translucentPropertyAsObject;
@end

@implementation UIView (_UITranslucentAppearanceProxyHack)

-(void)setTranslucentPropertyAsObject:(NSNumber *)translucentPropertyAsObject {
    if([self respondsToSelector:@selector(setTranslucent:)]) {
        id<_UITranslucentThingHack> trans = (id)self;
        trans.translucent = [translucentPropertyAsObject boolValue];
    }
}

-(NSNumber*)translucentPropertyAsObject {
    if([self respondsToSelector:@selector(setTranslucent:)]) {
        id<_UITranslucentThingHack> trans = (id)self;
        return @(trans.translucent);
    }
    return nil;
}

@end



回答3:


I solved it with my own category so that I can still use UIAppearance in something like the normal manner.

@interface UINavigationBar (MMTranlucenceUIAppearance)
@property(nonatomic,assign,getter=isMMTranslucent) NSInteger LYTranslucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR;  // Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
@end


@implementation UINavigationBar (MMTranlucenceUIAppearance)

// it appears that UIAppearance fails with BOOL
-(NSInteger)isMMTranslucent
{
    return self.translucent ? 0 : 1;
}

-(void)setMMTranslucent:(NSInteger)translucent
{
    self.translucent = (translucent == 0) ? NO : YES;
}

@end



回答4:


Dont know what about the iOS 7 .But in iOS6, according to the documentation you cannot set the translucent property to the UIAppearance object of the UINavigationBar. Some time it shows the all the possibilities in the autocomplete with unsupported one also



来源:https://stackoverflow.com/questions/17480796/uiappearance-settranslucent-error-illegal-property-type-c-for-appearance-sette

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!