【Xcode/Swift】SafeAreaを取得する


コード

今回はStoryboardには何も追加しなくてOKです

import UIKit

class ViewController: UIViewController {

    private var topSafeAreaHeight: CGFloat = 0
    private var bottomSafeAreaHeight: CGFloat = 0
    private var topSafeAreaLabel: UILabel!
    private var bottomSafeAreaLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
    }

    private func setupView() {
        // Top Safe Area 下のラベル
        topSafeAreaLabel = UILabel()
        topSafeAreaLabel.backgroundColor = UIColor.orange
        topSafeAreaLabel.text = "Top Safe Area"
        topSafeAreaLabel.textAlignment = NSTextAlignment.center
        topSafeAreaLabel.textColor = UIColor.white
        topSafeAreaLabel.font = UIFont.boldSystemFont(ofSize: 20)
        self.view.addSubview(topSafeAreaLabel)

        // Bottom Safe Area 上のラベル
        bottomSafeAreaLabel = UILabel()
        bottomSafeAreaLabel.backgroundColor = UIColor.purple
        bottomSafeAreaLabel.text = "Bottom Safe Area"
        bottomSafeAreaLabel.textAlignment = NSTextAlignment.center
        bottomSafeAreaLabel.textColor = UIColor.white
        bottomSafeAreaLabel.font = UIFont.boldSystemFont(ofSize: 20)
        self.view.addSubview(bottomSafeAreaLabel)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if #available(iOS 11.0, *) {
            // viewDidLayoutSubviewsでSafeAreaの取得を行う
            topSafeAreaHeight = self.view.safeAreaInsets.top
            bottomSafeAreaHeight = self.view.safeAreaInsets.bottom
            print("Top SafeArea: \(topSafeAreaHeight)")
            print("Bottom SafeArea: \(bottomSafeAreaHeight)")

            let width: CGFloat = self.view.frame.width
            let height: CGFloat = self.view.frame.height
            let labelHeight: CGFloat = 50
            topSafeAreaLabel.frame = CGRect(
                x: 0, y: topSafeAreaHeight,
                width: width, height: labelHeight)
            bottomSafeAreaLabel.frame = CGRect(
                x: 0, y: height - (labelHeight + bottomSafeAreaHeight),
                width: width, height: labelHeight)
        }
    }

}

この記事は役に立ちましたか?

はい
いいえ
貴重なフィードバックありがとうございます!