当前位置:网站首页>Swift - add navigation bar
Swift - add navigation bar
2022-07-05 12:08:00 【RRJia】
Preface :iOS13 in the future ,APP Lifecycle management of is managed by AppDelegate Turned into AppDelegate and SceneDelegate Two , And SceneDelegate Higher priority , in other words , When the project exists at the same time AppDelegate and SceneDelegate when , What plays a major role is SceneDelegate; When SceneDelegate When there is no , Still by AppDelegate Lifecycle management . therefore , When we add the navigation bar , It can be done from SceneDelegate Starting with , You can also get it from AppDelegate Starting with , Next , Two methods will be introduced respectively .
Method 1 : modify SceneDelegate file (SceneDelegate Can be carried out APP Split screen multi window management )
Step one : modify SceneDelegate The file is the following code :
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
// Set the background color of the window to white
self.window?.backgroundColor = .white
// take “ Custom view controller (ViewController())” Add to “ Navigation controller UINavigationController” in
let nav = UINavigationController(rootViewController: ViewController())
// Set the of the window “ Root view controller ” For the navigation controller customized above nav
self.window?.rootViewController = nav
}
}
Step two : establish ViewController.swift file , Add the following code , there ViewController.swift Is the above SceneDelegate Add to nav Of “ Custom view controller ”.( This step can be rewritten according to personal needs , Here are just examples )
import Foundation
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Set the background color of the navigation bar to gray
self.navigationController?.navigationBar.backgroundColor = .gray
// Set the title of the navigation bar to “ home page ”
self.navigationItem.title = " home page "
// Custom buttons
let tableBtn = UIButton(frame: CGRect(x: 100, y: 200, width: 150, height: 50))
// Set the background color of the button to green
tableBtn.backgroundColor = .green
// Set the title of the button to “ Button 1”
tableBtn.setTitle(" Button 1", for: .normal)
// Set the title color of the button to black
tableBtn.setTitleColor(.black, for: .normal)
// Add click event for button , That is to call clickTableBtn() Method
tableBtn.addTarget(self, action: #selector(clickTableBtn), for: .touchUpInside)
// Add a button to the view to make it appear
self.view.addSubview(tableBtn)
}
@objc func clickTableBtn() {
let vc = TestNavController()
// Set the navigation to jump to TestNavController page
self.navigationController?.pushViewController(vc, animated: true)
}
}
Step three : establish TestNavController.swift file , And add the following code ( This step can be rewritten according to personal needs , Here are just examples ):
import Foundation
import UIKit
class TestNavController: UIViewController {
override func viewDidLoad() {
self.view.backgroundColor = .yellow
}
}
Method 2 : modify AppDelegate file
Step one : modify AppDelegate The file is the following code :
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// take “ Custom view controller (ViewController())” Add to “ Navigation controller UINavigationController” in
let nav = UINavigationController(rootViewController: ViewController())
// New window
self.window = UIWindow(frame: UIScreen.main.bounds)
// Set the background color of the window to white
self.window?.backgroundColor = .white
// Set the of the window “ Root view controller ” For the navigation controller customized above nav
self.window?.rootViewController = nav
// Set the window to the front of the screen
self.window?.makeKeyAndVisible()
return true
}
}
Step two : establish ViewController.swift file , Add the following code , there ViewController.swift Is the above AppDelegate Add to nav Of “ Custom view controller ”.( This step is exactly the same as step 2 of method 1 , It can be rewritten according to personal needs , Here are just examples )
import Foundation
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Set the background color of the navigation bar to gray
self.navigationController?.navigationBar.backgroundColor = .gray
// Set the title of the navigation bar to “ home page ”
self.navigationItem.title = " home page "
// Custom buttons
let tableBtn = UIButton(frame: CGRect(x: 100, y: 200, width: 150, height: 50))
// Set the background color of the button to green
tableBtn.backgroundColor = .green
// Set the title of the button to “ Button 1”
tableBtn.setTitle(" Button 1", for: .normal)
// Set the title color of the button to black
tableBtn.setTitleColor(.black, for: .normal)
// Add click event for button , That is to call clickTableBtn() Method
tableBtn.addTarget(self, action: #selector(clickTableBtn), for: .touchUpInside)
// Add a button to the view to make it appear
self.view.addSubview(tableBtn)
}
@objc func clickTableBtn() {
let vc = TestNavController()
// Set the navigation to jump to TestNavController page
self.navigationController?.pushViewController(vc, animated: true)
}
}
Step three : establish TestNavController.swift file , And add the following code :( This step is exactly the same as step 3 of method 1 , It can be rewritten according to personal needs , Here are just examples )
import Foundation
import UIKit
class TestNavController: UIViewController {
override func viewDidLoad() {
self.view.backgroundColor = .yellow
}
}
Step four : Delete... From the project Scene Other relevant codes , Otherwise, the screen will be black ( Referable Xcode Black screen solution )
1) Delete SceneDelegate file ;
2) Delete Info file -> Information Property List -> Application Scene Manifest a
thus , The above two methods can be realized as follows “ Navigation effects ”, As shown in the figure below :
边栏推荐
- 互联网公司实习岗位选择与简易版职业发展规划
- redis集群中hash tag 使用
- 7月华清学习-1
- 15 methods in "understand series after reading" teach you to play with strings
- 强化学习-学习笔记3 | 策略学习
- Redirection of redis cluster
- 多表操作-自关联查询
- Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
- [configuration method of win11 multi-user simultaneous login remote desktop]
- Mmclassification training custom data
猜你喜欢
Riddle 1
Uniapp + unicloud + Unipay realize wechat applet payment function
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
你做自动化测试为什么总是失败?
[singleshotmultiboxdetector (SSD, single step multi frame target detection)]
[cloud native | kubernetes] actual battle of ingress case (13)
mysql拆分字符串做条件查询
Reading notes of growth hacker
【yolov5.yaml解析】
Simply solve the problem that the node in the redis cluster cannot read data (error) moved
随机推荐
Codeworks 5 questions per day (1700 average) - day 5
Halcon 模板匹配实战代码(一)
15 methods in "understand series after reading" teach you to play with strings
Vscode shortcut key
Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle
Pytorch softmax regression
【PyTorch预训练模型修改、增删特定层】
[cloud native | kubernetes] actual battle of ingress case (13)
一类恒等式的应用(范德蒙德卷积与超几何函数)
Acid transaction theory
13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system
Hiengine: comparable to the local cloud native memory database engine
【L1、L2、smooth L1三类损失函数】
JS for loop number exception
[yolov3 loss function]
How to make your products as expensive as possible
2022年国内云管平台厂商哪家好?为什么?
Use and install RkNN toolkit Lite2 on itop-3568 development board NPU
Shell script file traversal STR to array string splicing
Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)