[iOS] How to make UI parts added from code fit in Safe Area

manner

Prior to iOS 11, use a combination of topLayoutGuide and bottomLayoutGuide. For iOS 11 or later, it is integrated into the SafeAreaLayoutGuide, so use that.

ExampleViewController.swift


private let greenView = UIView()

private func setupView() {
  greenView.translatesAutoresizingMaskIntoConstraints = false
  greenView.backgroundColor = .green
  view.addSubview(greenView)

  let margins = view.layoutMarginsGuide
  NSLayoutConstraint.activate([
    greenView.leadingAnchor.constraint(equalTo: margins.leadingAnchor),
    greenView.trailingAnchor.constraint(equalTo: margins.trailingAnchor)
    ])

  if #available(iOS 11, *) {
    let guide = view.safeAreaLayoutGuide
    NSLayoutConstraint.activate([
     greenView.topAnchor.constraintEqualToSystemSpacingBelow(guide.topAnchor, multiplier: 1.0),
     guide.bottomAnchor.constraintEqualToSystemSpacingBelow(greenView.bottomAnchor, multiplier: 1.0)
     ])
  } else {
     let standardSpacing: CGFloat = 8.0
     NSLayoutConstraint.activate([
     greenView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: standardSpacing),
     bottomLayoutGuide.topAnchor.constraint(equalTo: greenView.bottomAnchor, constant: standardSpacing)
     ])
  }

If you are not using AutoLayout, you can get the margin value of the safeArea part from safeAreaInsets, and if it is less than iOS11, you can get it from topLayoutGuide.length, bottomLayoutGuide.length.

reference

iPhone X compatible ~ outside Safe Area ~ Positioning Content Relative to the Safe Area USE YOUR LOAF - Safe Area Layout Guide Stack Overflow - How do I use Safe Area Layout programmatically?

Recommended Posts

[iOS] How to make UI parts added from code fit in Safe Area
[IOS] How to get data from DynamoDB
How to call Swift 5.3 code from Objective-C
How to color code console output in Eclipse
How to get Class from Element in Java
How to make a follow function in Rails
How to make an image partially transparent in Processing
How to implement UICollectionView in Swift with code only
How to display a browser preview in VS Code
How to launch Swagger UI and Swagger Editor in Docker
How to make shaded-jar
I tried to make an application in 3 months from inexperienced
Let's write how to make API with SpringBoot + Docker from 0
How to specify character code and line feed code in JAXB
[iOS] [Objective-C] How to update a widget from an Objective-C app
[IOS] How to get the table name from AWS DynamoDB
How to set character code and line feed code in Eclipse
How to store a string from ArrayList to String in Java (Personal)
How to select a specified date by code in FSCalendar
[Swift5] How to avoid applying dark mode (dark appearance) in code
[IOS14] How to get Data type image data directly from PHPickerViewController?
[Docker context] ~ How to access docker in remote environment from VScode ~
How to change BackgroundColor etc. of NavigationBar in Swift UI
[Integration test code] How to select an element from date_select
How to apply C code format from the command line