LoginSignup
6
5

More than 5 years have passed since last update.

【Swift】ナビゲーションバーをステータスバーまで広げる

Posted at

1.はじめに

UINavigationController 配下ではないViewController に自分でUINavigationBar を設置する場合、ナビゲーションバーがステータスバーの領域までは伸びず、大変かっこ悪い。

このような感じ。

スクリーンショット 2018-09-29 17.17.24.png

このナビゲーションバーをステータスバーの領域まで伸ばします。

2.Storyboard

StoryboardでUINavigationBar を設置します。
ステータスバーにはかぶせないようにします。

今回は見やすいようにStyleをBlackにしています。

スクリーンショット 2018-09-29 17.17.03.png

3.コード

class ViewController: UIViewController, UINavigationBarDelegate {

    @IBOutlet weak var navigationBar: UINavigationBar!

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationBar.delegate = self
    }

    func position(for bar: UIBarPositioning) -> UIBarPosition {
        return .topAttached
    }
}

UINavigationBarDelegate に準拠します。
position(for:).topAttached を返します。

4.完成

こうなります。

スクリーンショット 2018-09-29 17.17.58.png

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5