在我的UIViewController类中,我将创建一个名为UIViewsafeAreaView并将其作为子视图添加到UIViewControllers view属性。我这样做是为了safeAreaView占用UIViewControllers view属性的整个安全区域:

- (void) viewDidLoad
{
    [self setToolbarWithColor: self.mainToolbarColor animated:NO];

    self.tapGestureRecognizer.delegate = self;
    self.view.clipsToBounds = YES;
    self.view.backgroundColor = [UIColor lightGrayColor];

    self.safeAreaView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.safeAreaView.clipsToBounds = YES;
    self.safeAreaView.delegate = self;
    self.safeAreaView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview: self.safeAreaView];

    [self.safeAreaView.leadingAnchor constraintEqualToAnchor: self.view.safeAreaLayoutGuide.leadingAnchor].active = YES;
    [self.safeAreaView.trailingAnchor constraintEqualToAnchor: self.view.safeAreaLayoutGuide.trailingAnchor].active = YES;
    [self.safeAreaView.topAnchor constraintEqualToAnchor: self.view.safeAreaLayoutGuide.topAnchor].active = YES;
    [self.safeAreaView.bottomAnchor constraintEqualToAnchor: self.view.safeAreaLayoutGuide.bottomAnchor].active = YES;

    [self.safeAreaView loadSubviews];
}


这很好用,但是我的问题是,在UIViewControllers初始化周期之后的某个时候,safeAreaView更新以说明状态栏(y位置向上移动20,尺寸减小20)。

我需要在safeAreaView上布置一些子视图,我不知道合适的时间?如果我像上面那样附加子视图,则它们的高度错误。而且我不能在子视图上使用某些自动布局功能,因为我需要做一些特定的事情。我也尝试过在viewWillAppear中执行上述代码,但是没有运气。

想知道是否有人有任何建议吗?

最佳答案

您可以在- (void)layoutSubviews类上覆盖safeAreaView

- (void)layoutSubviews {
    [super layoutSubviews];

    // Manual frame adjustment for non-autolayout participating subviews
    // ...
}


另一个选择是覆盖safeAreaView的类框架设置器,因此,每次视图框架更改时,您将有机会根据需要手动设置任何子视图框架。

10-08 07:30