The UINavigationBar is the most commonly used UINavigationBar at the top of iOS app screens.
Class hierarchy of UINavigationBar.
NSObject
↑upside down
UIResponder.
↑upside down
UIView.
↑upside down
UINavigationBar
AppleDeveloper Reference UINavigationBar
UINavigationBar example sentence
Generate a UINavigationBar and put a title and button on it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
//画面横サイズを取得 | |
let viewWidth = self.view.frame.width | |
//UINavigationBarを作成 | |
let myNavBar = UINavigationBar() | |
//UINavigationBarの位置とサイズを指定 | |
myNavBar.frame = CGRect(x: 0, y: 22, width: viewWidth, height: 40) | |
//ナビゲーションバーの色を変える | |
myNavBar.barTintColor = UIColor.gray | |
//ナビゲーションボタンの色を変更する | |
//UINavigationBar.appearance().tintColor = UIColor.black | |
//曇りガラスの効果を消す | |
myNavBar.isTranslucent = false | |
//ナビゲーションアイテムを作成 | |
let myNavItems = UINavigationItem() | |
myNavItems.title = "バーのタイトル" | |
//バーの左側に設置するボタンの作成 | |
let leftNavBtn = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(leftBarBtnClicked(sender:))) | |
myNavItems.leftBarButtonItem = leftNavBtn | |
//バーの右側に設置するボタンの作成 | |
let rightNavBtn = UIBarButtonItem() | |
//ボタンにする画像を選択する | |
let rightNavBtnImg:UIImage = UIImage(named:"swiftIcon_40")! | |
rightNavBtn.image = rightNavBtnImg | |
//ボタンが押され時のアクションを設定する | |
rightNavBtn.action = #selector(rightBarBtnClicked(sender:)) | |
myNavItems.rightBarButtonItem = rightNavBtn; | |
//作成したNavItemをNavBarに追加する | |
myNavBar.pushItem(myNavItems, animated: true) | |
self.view.addSubview(myNavBar) | |
} | |
//左側のボタンが押されたら呼ばれる | |
internal func leftBarBtnClicked(sender: UIButton){ | |
print("leftBarBtnClicked") | |
} | |
//右側のボタンが押されたら呼ばれる | |
internal func rightBarBtnClicked(sender: UIButton){ | |
print("rightBarBtnClicked") | |
} | |
} |