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

时间:2013-10-11 00:20:47

标签: iphone objective-c layout ios7

我有一个View Controller,它通过loadView方法以编程方式创建视图。视图的目的是显示数据模型内容。加载视图时,它在状态和导航栏下正确对齐。

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

请注意,我不使用自动布局,因为必须支持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

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

Screen layout after second load

使用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);
}

结果:

enter image description here

6 个答案:

答案 0 :(得分:14)

为同样的问题添加以下代码,对我来说可能是它的帮助

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

答案 1 :(得分:3)

这只发生在iOS 7上吗?

尝试一下:

self.navigationController.navigationBar.translucent = NO;

答案 2 :(得分:2)

请尝试以下操作:

 Toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

答案 3 :(得分:1)

试试这个

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];

答案 4 :(得分:1)

我认为问题是由于scrollview的内容插件的自动设置不正确。

尝试以下方法。在loadView中,将self.automaticallyAdjustsScrollViewInsets设置为NO(仅当NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1时)。现在,根据操作系统版本将contentInsetscrollIndicatorInsets设置为正确的值:

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

答案 5 :(得分:0)

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