GRW
GRW

Reputation: 615

iOS 7 crash, iOS 6 OK - UINavigationBar: -[UILabel setTitleTextAttributes:]: unrecognized selector sent to instance

I have an app that I was working on a long time ago and never completed. Recently started working on it again and I have run into a problem.

On iOS 6 this code works fine:

UINavigationBar *modalNavBar = [editView.subviews objectAtIndex:0];

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor, nil];
[modalNavBar setTitleTextAttributes:dict];

but on iOS 7 it crashes and gives the following error:

-[UILabel setTitleTextAttributes:]: unrecognized selector sent to instance

The method is clearly recognized since it comes up in the list of methods available on the UINavigationBar object.

I have read something about using NSForegroundColorAttributeName instead of UITextAttributeTextColor but that does not solve the problem on iOS 7, and on iOS 6 it results in the color not being applied.

Any suggestions on a solution to this issue?

Upvotes: 0

Views: 347

Answers (2)

azsromej
azsromej

Reputation: 1629

Try getting the navigation bar using a documented property. For example, navigationController.navigationBar.titleTextAttributes = textAttributes;. While one release of iOS may place the navigation bar at index 0 of the subviews array, another may add another view such that the navigationBar is no longer at that index.

Upvotes: 0

user2338215
user2338215

Reputation: 831

Perhaps it is a difference in how subviews are ordered in iOS7 and iOS6? I notice the error says that UILabel doesn't have the selector setTitleTextAttributes:, and indeed it does not. But your code indicates that you are expecting the object to be a UINavigationBar... but apparently it is a UILabel instead. So...

UINavigationBar *modalNavBar = [editView.subviews objectAtIndex:0];

...is returning the wrong type of object. For some reason you've got a UILabel at index 0 when you run on iOS7, but a UINavigationBar at that index when you run on iOS6.

Upvotes: 1

Related Questions