4

I have some old iOS code and now I'm migrating to sdk7. I have to support iOS 5 (so no auto layout). Code creates some parts of UI dynamically (by code).

To handle possibility that navigation view can overlap with navigation bar I did this code:

-(void) viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];

  CGRect navBarRect = self.navigationController.navigationBar.bounds; // 1
  CGPoint bottomLeft = [self.navigationController.navigationBar convertPoint: CGPointMake(0, CGRectGetMaxY(navBarRect))
                                                                      toView: self.view]; // 2
  navBarRect = [self.navigationController.navigationBar convertRect: navBarRect
                                                             toView: self.view]; // 3

  DDLogVerbose(@"Test positions bottomLeft=%@ navBarRect=%@",
               NSStringFromCGPoint(bottomLeft), NSStringFromCGRect(navBarRect));

  CGFoat localTop = CGRectMaxY(navBarRect);
  CGRect frame = self.view.bounds;
  ...

This localTop suppose to give me proper y position where I can start add my items independent on iOS version and suppose to be resistant to any new Apple inventions.

But this doesn't work. Line marked as 1 gives proper bounds ({0, 0}, {320, 44}). But then after executing line 3 top left corner of navBarRect is {0, 20} (self.view overlaps with navigation bar and status bar so this is expected) but its size is {640, 88} so double of that what is expected. I also tried version with convertPoint:toView (line 2) but this give me same result.

I've checked all superviews of navigation and and self.view up to the common view and I didn't found any transformation so how it is possible that this frame changed size after line 3?

(lldb) po self.view
<UIView: 0x1d9ac1f0; frame = (0 0; 320 411); layer = <CALayer: 0x1d9acae0>>
(lldb) po [self.view superview]
<UIViewControllerWrapperView: 0x1d9b5b20; frame = (0 20; 320 411); layer = <CALayer: 0x1d9b5b80>>
(lldb) po [[self.view superview] superview]
<UINavigationTransitionView: 0x1d9aa040; frame = (0 0; 320 431); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x1cd2a470>>
(lldb) po [[[self.view superview] superview] superview]
<UILayoutContainerView: 0x1cd207c0; frame = (0 0; 320 431); autoresize = W+H; layer = <CALayer: 0x1d975980>>
(lldb) po [self.navigationController navigationBar]
<UINavigationBar: 0x1cd08d60; frame = (0 20; 320 44); clipsToBounds = YES; opaque = NO; autoresize = W; gestureRecognizers = <NSArray: 0x1cd09400>; layer = <CALayer: 0x1cd09070>>
(lldb) po [[self.navigationController navigationBar] superview]
<UILayoutContainerView: 0x1cd207c0; frame = (0 0; 320 431); autoresize = W+H; layer = <CALayer: 0x1d975980>>

Or maybe there is better solution (no hard coding)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 1
    If you do this in `- (void)viewDidAppear:(BOOL)animated`, it will work. So I guess that maybe something is not ready in `viewWillAppear `. – KudoCC Jan 28 '14 at 14:04
  • 1
    interesting! When I moved it to `viewDidAppear` it works. I think it will be better when I will do it in `viewDidLayoutSubviews`. Big thanks for the clue. – Marek R Jan 28 '14 at 14:37

0 Answers0