当loadView再次调用时,iOS7视图会在状态/导航栏下移动

时间:2023-01-22 22:49:20

I have a View Controller that creates its view programatically through the loadView method. The purpose of the view is to display a data models contents. When the view loads it is aligned correctly under the status and navigation bar.

我有一个视图控制器,它通过loadView方法程序化地创建它的视图。视图的目的是显示数据模型内容。当视图加载时,它在状态栏和导航栏下被正确对齐。

But at a later point I present another view modally to allow the user to select a different data model to populate the view. This causes the the loadView to get called again and re-create the required UI for the new data model. The problem is that the view's contents now appear under the status and navigation bars!

但稍后,我将以模态方式呈现另一个视图,以允许用户选择不同的数据模型来填充该视图。这将导致重新调用loadView并为新数据模型重新创建所需的UI。问题是视图的内容现在出现在状态栏和导航栏下!

Please note that I am not using auto-layout due to having to support iOS4 :( Also if I include the extendedLayout fix - it does fix the problem but only after the modal's dismiss animation completes leaving a jump down effect. My code below, thanks for any help.

请注意,我没有使用自动布局,因为我必须支持iOS4:(另外,如果我包含了extendedLayout修复——它确实修复了问题,但只有在模式的解散动画完成后,才会产生跳转向下的效果。我下面的代码,谢谢你的帮助。

- (void)loadView {

CGRect frame = CGRectMake(0, 0, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
self.view.backgroundColor = [Constants categoriesScreenBackgroundColor];

CGRect scrollFrame = CGRectMake(0, 0, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight - ToolbarHeight);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
        UIViewAutoresizingFlexibleBottomMargin |
        UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:scrollView];

_toolbarViewController = [self createToolbarViewController];
[self.view addSubview:_toolbarViewController.view];
_toolbarViewController.productInfoWorkflowState = _productInfoWorkflowState;

UIView *containerView = [[UIView alloc] initWithFrame:scrollView.frame];
containerView.backgroundColor = [UIColor clearColor];

_headerViewController = [self createHeaderViewController];
[containerView addSubview:_headerViewController.view];

_menuViewController = [[ProductInfoMenuViewController alloc] initWithBatch:[self batchData]];
_menuViewController.delegate = self;
[containerView addSubview:_menuViewController.view];

CGRect categoriesFrame = _menuViewController.view.frame;
categoriesFrame.origin.y = _headerViewController.view.frame.size.height;
_menuViewController.view.frame = categoriesFrame;

CGRect viewFrame = containerView.frame;
viewFrame.size.height = _headerViewController.view.frame.size.height + _menuViewController.view.frame.size.height;
containerView.frame = viewFrame;

[scrollView addSubview:containerView];
scrollView.contentSize = containerView.frame.size;

_starViewController = [[StarViewController alloc] initForProduct:_productData With:[StarredItems new]];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_starViewController.view];
}

Screen layout after first load:

第一次加载后的屏幕布局:

当loadView再次调用时,iOS7视图会在状态/导航栏下移动

Screen layout after second load:

第二次加载后的屏幕布局:

当loadView再次调用时,iOS7视图会在状态/导航栏下移动

With Leo's suggested fix, the scrollView is correct BUT the toolbar at the bottom now appears incorrectly. Code I used (placed after the toolbar creation code above):

使用Leo的建议修复,scrollView是正确的,但是底部的工具栏现在显示不正确。我使用的代码(放在上面工具栏创建代码之后):

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
    self.automaticallyAdjustsScrollViewInsets = NO;
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(64.0f, 0.0f, 0.0f, 0.0f);
    scrollView.contentInset = UIEdgeInsetsMake(64.0f, 0.0f, 0.0f, 0.0f);
}

Results:

结果:

当loadView再次调用时,iOS7视图会在状态/导航栏下移动

6 个解决方案

#1


14  

Add following code its work for me for same issue, may be its help you

添加以下代码为我的工作为同样的问题,可能是它的帮助你

/* ios 7 Change */
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

#2


3  

Does this only happen on iOS 7?

这只发生在ios7上吗?

Give this a try:

给这一个尝试:

self.navigationController.navigationBar.translucent = NO;

#3


2  

Try following:

试一试:

 Toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

#4


1  

try this

试试这个

CGRect frame = CGRectMake(0, StatusBarHeight, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
self.view.backgroundColor = [Constants categoriesScreenBackgroundColor];

CGRect scrollFrame = CGRectMake(0, StatusBarHeight, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight - ToolbarHeight);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
        UIViewAutoresizingFlexibleBottomMargin |
        UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:scrollView];

#5


1  

I think the problem is due to incorrect automatic set of content insets of the scrollview.

我认为问题是由于scrollview的内容设置不正确导致的。

Try the following. In your loadView, set self.automaticallyAdjustsScrollViewInsets to NO (only if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1). Now, set the contentInset and scrollIndicatorInsets to the correct values depending on OS version:

试试下面的。在loadView中,设置self。不需要(只有NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_ 1)。现在,根据OS版本将contentInset和scrollIndicatorInsets设置为正确的值:

scrollview.contentInset = scrollview.scrollIndicatorInsets = UIEdgeInsetMake(NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1 ? 0 : 64, 0, 0, 0);

#6


0  

My problem was solved unmarking the check "Adjust Scroll View Insets" in the View Controller (using Storyboards).

我的问题解决了在视图控制器(使用故事板)中取消勾选“调整滚动视图内嵌”的标记。

#1


14  

Add following code its work for me for same issue, may be its help you

添加以下代码为我的工作为同样的问题,可能是它的帮助你

/* ios 7 Change */
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

#2


3  

Does this only happen on iOS 7?

这只发生在ios7上吗?

Give this a try:

给这一个尝试:

self.navigationController.navigationBar.translucent = NO;

#3


2  

Try following:

试一试:

 Toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

#4


1  

try this

试试这个

CGRect frame = CGRectMake(0, StatusBarHeight, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = YES;
self.view.backgroundColor = [Constants categoriesScreenBackgroundColor];

CGRect scrollFrame = CGRectMake(0, StatusBarHeight, [Constants ScreenWidth], [Constants ScreenHeight] - StatusBarHeight - ToolbarHeight - ToolbarHeight);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight |
        UIViewAutoresizingFlexibleBottomMargin |
        UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:scrollView];

#5


1  

I think the problem is due to incorrect automatic set of content insets of the scrollview.

我认为问题是由于scrollview的内容设置不正确导致的。

Try the following. In your loadView, set self.automaticallyAdjustsScrollViewInsets to NO (only if NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1). Now, set the contentInset and scrollIndicatorInsets to the correct values depending on OS version:

试试下面的。在loadView中,设置self。不需要(只有NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_ 1)。现在,根据OS版本将contentInset和scrollIndicatorInsets设置为正确的值:

scrollview.contentInset = scrollview.scrollIndicatorInsets = UIEdgeInsetMake(NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1 ? 0 : 64, 0, 0, 0);

#6


0  

My problem was solved unmarking the check "Adjust Scroll View Insets" in the View Controller (using Storyboards).

我的问题解决了在视图控制器(使用故事板)中取消勾选“调整滚动视图内嵌”的标记。