views:

246

answers:

4

Any clues why my programmatically defined UIScrollView (using the application frame) always leaves an empty space at the top (below the navigationBar) of 20 pixels. How can I close that?

UIScrollView *vScrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
vScrollView.backgroundColor = [UIColor redColor];

My screen looks like this:

  • status bar
  • -- Navigation bar
  • --- the mysterious gap (10px?)
  • ---- my Scrollviewcontent (with red background color)

How do I need to call the application frame size to avoid that gap?

A: 

What does your application tells in info.plist file about using status bar? Is it enabled or disabled? If I understand correctly, you look at status bar's 20 pixels.

Alexander Babaev
it's not the status bar but a gap below the navigation bar. An NSLog on [[UIScreen mainScreen] applicationFrame].origin.y returns 20.
iFloh
+1  A: 

Are you hiding the status bar in your app delegate?

- (void) applicationDidFinishLaunching:(UIApplication *)application {
    [application setStatusBarHidden:YES];
    ...
}
Alex Reynolds
No, the status bar is visible. Thinking further into it I realise the 20 pixels is most likely my navigation bar.My screen looks like this:- status bar-- Navigation bar--- the mysterious gap (10px?)---- my Scrollviewcontent (with red background color)
iFloh
Your navigation bar will be more than 20 pixels high, unless you have customized it somehow. The status bar is 20 pixels high and is probably a good candidate for troubleshooting.
Alex Reynolds
I now overrode the [[UIScreen mainScreen] applicationFrame].origin.y to zero and that does the trick, even though my guts tell me that's not a clean approach ...
iFloh
I'm not positive, but I suspect you have not set the status bar to be hidden either programmatically or through Interface Builder, and your programmatic or IB elements are framed with an offset that accounts for the status bar.
Alex Reynolds
A: 

I found I use the wrong frame coordinates "applicationFrame". All works fine when changing it to the bounds.

UIScrollView *vScrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
iFloh
+2  A: 

UIScreen-applicationFrame is the full screen rectangle with the status bar (clock) removed. Since your scroll view is inside the view area of the navigation controller, you need to use coordinates in the inner view's coordinate space.

Even using UIScreen-bounds isn't giving you quite the right result since that rect would be the full size of the screen including the space that should be taken by the navigation bars. Using -bounds, you'll find your UIScrollView actually runs 64 pixels off the bottom of the screen.

Assuming you're creating the scroll view in the UINavigationController's -loadView method, something like this should work:

- (void)loadView {
  [super loadView];
  UIScrollView *sv = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  [self.view addSubview:sv];
  [sv release];
}
pendor