iOS 9:以编程方式安全区域布局并在视图中删除侧面空间

时间:2018-04-24 05:02:31

标签: ios objective-c iphone xcode ios9.3

如何为我的应用添加安全区域布局 iPhone 5S iOS 9.3.5 。 我的代码是这样的

if (@available(iOS 11, *))
    {
        loginView = [[LoginView alloc] initWithFrame:self.view.safeAreaLayoutGuide.layoutFrame];
        [self.view addSubview:loginView];
        loginView.translatesAutoresizingMaskIntoConstraints = NO;

        UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
        [loginView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
        [loginView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
        [loginView.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
        [loginView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
    }
    else
    {
        loginView = [[LoginView alloc] initWithFrame:self.view.layoutMarginsGuide.layoutFrame];
        [self.view addSubview:loginView];
        loginView.translatesAutoresizingMaskIntoConstraints = NO;

        UILayoutGuide *margins = self.view.layoutMarginsGuide;
        [viloginViewew.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor].active = YES;
        [loginView.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor].active = YES;
        [loginView.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].active = YES;
        [loginView.bottomAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor].active = YES;

    }

我试图在我的设备中运行,但安全区域布局显示左侧和右侧的一些空间。我想删除那个空格,以便它以全宽显示。

我从layoutMarginsGuide得到的高度太长,所以它被隐藏在视野之外。如何获得layoutMarginsGuide的正确高度?

我是否实施了错误的代码?我该怎么办?

1 个答案:

答案 0 :(得分:0)

iPhone 5s对我有用。它覆盖了所有空间。用于iOS 11以下的iOS。

[self.view addConstraints:@[
    [NSLayoutConstraint constraintWithItem:loginView
                                 attribute:NSLayoutAttributeBottom
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.bottomLayoutGuide
                                 attribute:NSLayoutAttributeBottom
                                multiplier:1
                                  constant:0],
    [NSLayoutConstraint constraintWithItem:loginView
                                 attribute:NSLayoutAttributeTop
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.bottomLayoutGuide
                                 attribute:NSLayoutAttributeTop
                                multiplier:1
                                  constant:0],
    [NSLayoutConstraint constraintWithItem:loginView
                                 attribute:NSLayoutAttributeLeading
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.view
                                 attribute:NSLayoutAttributeLeading
                                multiplier:1
                                  constant:0],
    [NSLayoutConstraint constraintWithItem:loginView
                                 attribute:NSLayoutAttributeTrailing
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:self.view
                                 attribute:NSLayoutAttributeTrailing
                                multiplier:1
                                  constant:0]
                            ]]