2

我在 iOS 7 中看到了许多处理状态栏的令人困惑和糟糕的解决方案,但我喜欢 Apple 在 - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference中提到的以下解决方案/Reference.html#//apple_ref/doc/uid/TP40006926

....但它不起作用!此方法按预期调用,但 childView 没有向下移动 20 像素。

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        UIView *childView = self.childController.view;
        id topGuide = self.topLayoutGuide;
        CGFloat topBarOffset = self.topLayoutGuide.length; //I checked this valued is 20
        NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (childView, topGuide);
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"V:[topGuide]-20-[childView]"
                                                                          options: 0
                                                                          metrics: nil
                                                                            views: viewsDictionary
                                   ]
        ];
    }
}

以下对我有用,但我真的很想使用 topLayoutGuide 使用 AutoLayout 来解决这个问题。

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.backgroundColor = [UIColor blackColor];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        CGRect frame = self.bounds;
        frame.origin.y = 20;
        frame.size.height -= 20;
        self.childView.frame = frame;
    } else {
        self.childView.frame = self.bounds;
    }
}

任何想法为什么使用 topLayoutGuide 不起作用?谢谢。

4

0 回答 0