user
user

Reputation: 3456

Why does [[UINavigationBar appearance] setTranslucent:NO] crash my app?

Same question as this, but that question was shunned (because of NDA at the time) and is no longer active.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Illegal property type, c for appearance setter, _installAppearanceSwizzlesForSetter:'

I'm setting this in viewDidLoad of my initial view controller. setTranslucent comes up on autocomplete, and does not complain until crashing and talking about swizzles and things.

Any info on this would be great, I'm still having a very rough time getting a consistent status bar appearance across my app.

Upvotes: 40

Views: 17945

Answers (5)

Henson Fang
Henson Fang

Reputation: 1207

you crash because you use the illegal method. in UIAppearance , it says

To participate in the appearance proxy API, tag your appearance property selectors in your header with UI_APPEARANCE_SELECTOR.

it means when you use [[XXX appearance] method],the method method must have the attribute UI_APPEARANCE_SELECTOR,or it may throw an exception,and translucent does not have it.

but what puzzles me is that, [[XXX appearance] method] in iOS8 it's ok but crashes in iOS7 and apple document does not say it.

Upvotes: 2

hgwhittle
hgwhittle

Reputation: 9426

It seems that the translucent property just can't be set using UIAppearance. I don't know exactly why, but I guess some properties just aren't supported. However, I solved this by creating a custom UIViewController and making all other viewControllers in my app a subclass of that custom viewController. That way, I can set global properties (such as translucent in your case) that will be inherited by all other viewControllers in my app. I know that's kind of a big change, but I hope it helps.

**** EDIT ****

As of iOS 8, translucency can be set with UIAppearance:

Objective C

if([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {

    [[UINavigationBar appearance] setTranslucent:YES];
}

Swift

if (UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8.0 {

    UINavigationBar.appearance().translucent = true
}

Upvotes: 55

You can not change translucent property after class has been initialized.

[newsViewNavigationController.navigationBar setTranslucent:NO];

I did something like this and it worked!

Upvotes: 0

oky_sabeni
oky_sabeni

Reputation: 7822

I don't know the answer to your question and I got here from google but if you use navigation controllers, I can change all the translucency with this line:

[self.navController.navigationBar setTranslucent:NO];

Upvotes: 2

JulianB
JulianB

Reputation: 1686

You can fool it though by specifying a non exist image, which will amke the tool bar go opaque

[[UIToolbar appearance] setBackgroundColor:[UIColor colorWithRed:219.0/255.0 green:67.0/255.0 blue:67.0/255.0 alpha:1.0]];

[[UIToolbar appearance] setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

Upvotes: 8

Related Questions