UIAppearance setTranslucent错误:非法属性类型,c表示外观设置器,_installAppearanceSwizzleForSetter

时间:2013-07-05 03:49:48

标签: ios objective-c uinavigationbar uiappearance ios7

尝试使用iOS 7:

[[UINavigationBar appearance] setTranslucent:NO];

崩溃和错误:

*** 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

其他通话工作正常:例如。

[[UINavigationBar appearance] setBarStyle: UIBarStyleBlack];

当我在本地设置半透明时,它不会崩溃:

[self.navigationController.navigationBar setTranslucent:NO];

我认为这是针对iOS 7的,但在iOS 6中还没有尝试过相同的东西。

4 个答案:

答案 0 :(得分:17)

[[UINavigationBar appearance] setTranslucent:NO] 

它不适用于iOS 6.它仅适用于iOS 7以上版本。

答案 1 :(得分:1)

这是解决它的代码。问题是UIAppearance不适用于BOOL类型。这不应成为拒绝应用的理由,因为它使用标准(尽管是hacky)程序。玩得开心。

@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

答案 2 :(得分:1)

我用自己的类别解决了这个问题,这样我仍然可以像普通方式一样使用UIAppearance。

@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

答案 3 :(得分:0)

不知道iOS 7的内容。但在iOS6中,根据文档,您无法将半透明属性设置为UINavigationBar的UIAppearance对象。有一段时间它显示了自动完成中所有可能性,也有不受支持的