Thursday, 2 January 2020

Xcode 11: Safe Area code implementation using Objective-C not working

I am new to iOS application development and I am currently working on an existing iOS application written using Objective-C. I came across a requirement where I need to implement Safe Area programmatically. So I have implemented the following piece of code in "viewDidLoad" method.

if(@available(iOS 11, *)){
    UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
    [self.view.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
    [self.view.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
    [self.view.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
    [self.view.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
}else{
    UILayoutGuide * margins = self.view.layoutMarginsGuide;
    [self.view.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor].active = YES;
    [self.view.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor].active = YES;
    [self.view.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].active = YES;
    [self.view.bottomAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor].active = YES;
}

But, when I try to run the application connecting to iPhone XR iOS Simulator device, the above code is not working. The view is not getting started after the safe area in landscape mode instead it is getting started from the safe area itself due to which the text is getting cut off

Am I missing anything? Can you please suggest on what needs to be done so that the safe area can be implemented?



from Xcode 11: Safe Area code implementation using Objective-C not working

No comments:

Post a Comment