UIAppearance setTranslucent error: тип нелегального свойства, c для средства определения видимости, _installAppearanceSwizzleForSetter

Попытка 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.

Ответ 1

[[UINavigationBar appearance] setTranslucent:NO] 

Он недоступен iOS 6. Он доступен только в iOS 7.

Ответ 2

Здесь код, чтобы обойти его. Проблема в том, что UIAppearance не будет работать с типами BOOL. Это не должно быть основанием для отклонения приложения, поскольку оно использует стандартные (хотя и хакеры) процедуры. Получайте удовольствие.

@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

Я решил его со своей собственной категорией, чтобы я все еще мог использовать 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

Ответ 4

Не знаю, что относительно iOS 7. Но в iOS6, согласно документации, вы не можете установить свойство полупрозрачности для объекта UIAppearance UINavigationBar. Некоторое время он показывает все возможности в автозаполнении с неподдерживаемым также