1

I have a UIViewController that has a UIScrollView as a root view defined and added like this

[self.view addSubview:mainScrollView];
UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight)];

I offset by 64 to start from the NavigationController (20 status bar + 44 navigation bar) and it's all OK until now. Then I add another ViewController and when i go back the mainScrollView is offset in y by 64 points from the navigation controller. Why does this happen ?

Before:

enter image description here

After:

enter image description here

Desdenova
  • 5,326
  • 8
  • 37
  • 45
Elgert
  • 470
  • 2
  • 5
  • 19
  • If you're using a fullscreen `UIScrollView` with no other views in your view controller, you can set `self.view = mainScrollView` instead of using `addSubview:`, then the view controller should offset everything for you automatically... – jjv360 Jul 18 '14 at 12:41
  • I just tried what you suggested but everything disappears, even the navigation bar when i click back. I do have other views inside `mainScrollView` – Elgert Jul 18 '14 at 12:49
  • Are you doing anything on `viewWillDisappear`? Also, if setting `self.view`, are you doing it in `loadView`? – jjv360 Jul 18 '14 at 12:54
  • I dont have a `viewWillDisappear` and I'm setting self.view on `viewDidLoad` – Elgert Jul 18 '14 at 12:56
  • Try change `viewDidLoad` to `loadView`... – jjv360 Jul 18 '14 at 13:00
  • Its the same, the offset happens again – Elgert Jul 18 '14 at 13:04
  • Can you add a screenshot? – jjv360 Jul 18 '14 at 13:05
  • before http://storage.workupload.com/image/7EdPsvVs and after http://storage.workupload.com/image/NjDkMUce – Elgert Jul 18 '14 at 13:10
  • Is your navigation bar translucent? – Desdenova Jul 18 '14 at 13:11
  • No the bar itself. Try without offsetting the view manually like this `CGRectMake(0, 0, screenWidth, screenHeight)` – Desdenova Jul 18 '14 at 13:14
  • if I do that my cover picture starts behind the navigation bar and then when I come again here it starts exactly from the navigation bar – Elgert Jul 18 '14 at 13:16

4 Answers4

2

This is the same probably can see when you add a UITableView to a UIViewController that has Under Top Bars property checked

enter image description here

You can consider this a "hack" or not but try unchecking that box and see if it fixes your problem.

If it does, then it should be a matter of setting the proper EdgeInset. One you get that part fixed, you could then just re-enable "under top bars" if you actually want/need it.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Jonathon Hibbard
  • 1,547
  • 13
  • 20
1

Most probably is due to a wrong combination of those properties:

  1. automaticallyAdjustsScrollViewInsets
  2. edgesForExtendedLayout
  3. extendedLayoutIncludesOpaqueBars
  4. translucency of the navigation bar

The first one adds an inset, if the first view or first subview of the VC is a scrollview or a view that inherits from it (such as a tableview). The second tell the main view if it should lie under the navbar or tab bar that's where the "adjust" is useful.
Here a similar answer iOS 7 UITextView vertical alignment

Community
  • 1
  • 1
Andrea
  • 26,120
  • 10
  • 85
  • 131
  • I didnt use any of those 3 and my navigationbar is lightcontent not transluscent. – Elgert Jul 21 '14 at 07:19
  • Maybe you just think you aren't, some properties are set to YES by default, check on interface builder if you have the checkbox on automaticallyAdjustsScrollViewInsets, give it a shot.. or if you are doing programmatically, force it in init or viewDidLoad – Andrea Jul 21 '14 at 07:25
  • I did it :D it was the first one and also @Jonathon was helped with the line of code too – Elgert Jul 21 '14 at 07:38
0

Your Scroll view is started from y=64. It may be the cause offset of y=64 when you navigate on another view controller. Kinjal

0

It's rather hard to get what is the reason exactly.

What I would do is to implement the viewWillLayoutSubviews method of the UIViewController and break into it to get all the metrics of your self.view and your mainScrollView.documentation here

With those information we can find what can be the problem.

Either to try something to go around it or maybe find the root cause I would also try to set the mainScrollView as the UIViewController view:

- (void)loadView
{
    [super loadView];
    UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    self.view = mainScrollView;
}

Another solution can be to adjust the y offset of the mainScrollView in the viewWillLayoutSubviews method:

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.mainscrollView.frame = self.view.bounds;
}
gsempe
  • 5,371
  • 2
  • 25
  • 29