0

Sorry for simple question, but following code:

[[UINavigationBar appearance] setBackgroundImage:[UIImage
    imageNamed:@"backgroundNavigationBar.png"] forBarMetrics:UIBarMetricsDefault];
CGRect frame = [[UINavigationBar appearance] frame];
NSLog(@"%@", NSStringFromCGRect(frame));
UIView *view = [[UIView alloc] initWithFrame:frame];
[[UINavigationBar appearance] addSubview: view];

gives me

{{0, 0}, {0, 0}}

How to correctly get frame of current Navigation Bar with actual sizes, i.e. 320x64?

pmor
  • 5,392
  • 4
  • 17
  • 36

2 Answers2

1

Try:

CGRect frame = self.navigationController.navigationBar.frame;
0

I'm afraid your code won't work the way you'd like :/

The appearance proxy is certainly not for adding subviews !

You can only ever edit properties through UIAppearance that are marked as 'UI_APPEARANCE_SELECTOR' in the header files !

The compiler lets you write your code, because + (instancetype)appearance actually returns a proxy object of the class.

The actual view you want to add a subview to does not have anything to do with that proxy. UIAppearance looks at the proxy object, and copies over only the UI_APPEARANCE_SELECTOR marked properties when a new view is created.

Please read this https://developer.apple.com/library/ios/documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html

and this http://nshipster.com/uiappearance/ , it will give you a great overview of how UIAppearance is used.

CodingMeSwiftly
  • 3,231
  • 21
  • 33