Warning: Undefined array key 1 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 12
Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 15
Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 18
Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 21
Warning: Undefined array key 1 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 24
Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 27
Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 30
Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 33
クイズアプリを作っていて、ナビゲーションエリアに問題番号(UILabel)を表示したいなぁと思って、実装することになったので、その方法をメモとして残しておきます。
実装方法
やっていることは単純で、UILabelをnavigationBarのSubViewに追加しています。
override func viewDidLoad() {
super.viewDidLoad()
// ナビゲーションの右上にラベルをセット
if let navigationBar = self.navigationController?.navigationBar {
let labelFrame = CGRect(x: view.frame.size.width - 70 , y: 0, width: 55.0, height: navigationBar.frame.height)
let label = UILabel(frame: labelFrame) // ラベルサイズと位置
label.textColor = UIColor.white // テキストカラー
navigationBar.addSubview(label)
}
}
UILabelのサイズは幅を55、高さをナビゲーションの高さに設定しています。
ラベルの位置は右端から70戻ったところ、高さを0にしています。
実行結果
ナビゲーションバー の右上にラベルを表示できました。


