How to add a safe area programmatically

后端 未结 1 1834
别那么骄傲
别那么骄傲 2021-02-15 16:21

When you open the view, it will look like the image below,

i Phone x open view

i Phone 8 open view

For iphone x, I would like to add a safe area programm

相关标签:
1条回答
  • 2021-02-15 17:07

    Here is sample code for Safe Area Layout. Try this in Objective-C and see:

    UIView * myView = // initialize view using IBOutlet or programtically
    
    myView.backgroundColor = [UIColor red];
    myView.translatesAutoresizingMaskIntoConstraints = NO;
    
    UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
    [self.myView.leadingAnchor constraintEqualToAnchor:guide.leadingAnchor].active = YES;
    [self.myView.trailingAnchor constraintEqualToAnchor:guide.trailingAnchor].active = YES;
    [self.myView.topAnchor constraintEqualToAnchor:guide.topAnchor].active = YES;
    [self.myView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor].active = YES;
    
    // Refresh myView and/or main view
    [self.view layoutIfNeeded];
    //[self.myView layoutIfNeeded];
    

    Ref from: Use Safe Area Layout programmatically

    Result:

    0 讨论(0)
提交回复
热议问题