victorz
victorz

Reputation: 39

iOS 9 : Safe Area Layout Programmatically and Remove Side Space in View

How to add safe area layout to my app for iPhone 5S and iOS 9.3.5. my code is like this

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;

    }

I tried to run in my device but safe area layout show some space in side left and right. I want to remove that space so it will show in full width.

And the height I got from layoutMarginsGuide is too long so it being hidden from view. how to get the proper height of layoutMarginsGuide ?

Do i implement a code wrong ? What should i have to do ?

Upvotes: 0

Views: 1289

Answers (1)

SThakur
SThakur

Reputation: 282

It worked for me for iPhone 5s. It is covering all space. Use it for iOS below 11.

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

Upvotes: 0

Related Questions