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

标签 iphone objective-c layout ios7

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

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

请注意,由于必须支持 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

最佳答案

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

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

关于iphone - 当再次调用 loadView 时,iOS7 View 在状态/导航栏下移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19308325/

相关文章:

iphone - 在Objective-C中,与类方法相比,拥有单例有什么优势?

iphone - 推送通知-声音不起作用?

iphone - 如何防止 UILabel 被 '...' 切断

ios - 如何在右侧显示助理编辑器

iphone - 如何在 Xcode 中一次在多个目的地上 'Build & Run'?

javascript - 按升序/降序动态地将 z-index 分配给具有特定类别的未知数量的元素

swift - UIAlert 不再打开,Xcode 告诉我调试 NSLayoutConstraintToNilAnchor

css - 底部的 div 如何占用其余部分

ios - SocketRocket - 无效的 Sec-WebSocket-Accept

iOS 7 状态栏不在 View 顶部,无需编辑每个 View