当前位置:网站首页>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 :
边栏推荐
- Ncp1342 chip substitute pn8213 65W gallium nitride charger scheme
- Network five whip
- 【 YOLOv3中Loss部分计算】
- Sentinel sentinel mechanism of master automatic election in redis master-slave
- 1.php的laravel创建项目
- 无线WIFI学习型8路发射遥控模块
- 什么是数字化存在?数字化转型要先从数字化存在开始
- [singleshotmultiboxdetector (SSD, single step multi frame target detection)]
- Matlab imoverlay function (burn binary mask into two-dimensional image)
- 【Win11 多用户同时登录远程桌面配置方法】
猜你喜欢

Multi table operation - sub query

12.(地图数据篇)cesium城市建筑物贴图

Four operations and derivative operations of MATLAB polynomials

Splunk configuration 163 mailbox alarm

石油化工企业安全生产智能化管控系统平台建设思考和建议

13.(地图数据篇)百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换

【TFLite, ONNX, CoreML, TensorRT Export】

XML parsing

How to clear floating?

What is digital existence? Digital transformation starts with digital existence
随机推荐
Hash tag usage in redis cluster
[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]
Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
查看多台机器所有进程
石油化工企业安全生产智能化管控系统平台建设思考和建议
查看rancher中debug端口信息,并做IDEA Remote Jvm Debug
Mmclassification training custom data
11.(地图数据篇)OSM数据如何下载使用
自动化测试生命周期
15 methods in "understand series after reading" teach you to play with strings
JS for循环 循环次数异常
Open3d mesh (surface) coloring
13.(地图数据篇)百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
程序员内卷和保持行业竞争力
Codeforces Round #804 (Div. 2)
无线WIFI学习型8路发射遥控模块
Multi table operation - Auto Association query
Pytorch softmax regression
1 plug-in to handle advertisements in web pages