日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Swift5.x使用纯代码创建NavigationTab控制器设置启动图Wb第1部分

發布時間:2023/12/18 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Swift5.x使用纯代码创建NavigationTab控制器设置启动图Wb第1部分 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Swift5.x使用純代碼創建NavigationTab控制器設置啟動圖Wb第1部分

前言
1 使用純代碼創建NavigationTab控制器必須會,也可使用storyboard拖控件,沒有代碼創建靈活
2 學習swift基礎必須學習oc,個人建議,否則有點難。學好oc再來學swift里面有很多相似之處。swift不需要導入頭文件。數據類型非常嚴謹。否則報錯。定義類是全局的思想。依托強大的xcode可以提示你的報錯,有自動解決方法。如果函數過期,xcode也提示用什么替代了。非常不錯。拋開 2019年發布的swiftUI不說,那個跟前端框架React思想差不多,報錯不要緊,盡量嘗試,慢慢解決。
from Shanghai Johnson

這是基于swift 5語法寫的
先把啟動圖弄好

1 main.storyboard刪掉,紅框的Main刪掉

2.在資源目錄下新建一個啟動圖片 把啟動圖片拖進去

3 刪除 info.plist 刪除場景那個配置文件

4 選中項目屬性 找到launch 把那個置空 務必置空,否則報錯

5.找到編譯下的 搜索 asset 把圖片名稱填進去

6找到SceneDelegate.swift文件 書寫成員變量

var window: UIWindow? //加?是為了可選類型,可選項 ,其語法跟go類似 變量類型在后面

7 。刪除其他無關重寫函數 保留application函數

// AppDelegate.swift // SinaWeiBo // Created by 魯軍 on 2021/3/13. import UIKit @main class AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow?func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {window = UIWindow(frame: UIScreen.main.bounds)window?.backgroundColor = UIColor.white // window?.backgroundColor = UIColor.graywindow?.rootViewController = MainViewController()window?.makeKeyAndVisible()return true} }

8.新建一個MainViewController.swift文件 繼承自 UITabBarController
HomeTableViewController 。MessageTableViewController,DiscoverTableViewController,ProfileTableViewController 類似。

import UIKit // MARK:- 程序入口 class MainViewController: UITabBarController {override func viewDidLoad() {super.viewDidLoad()self.addChildViewControllers()setupComposedButton()}override func viewWillAppear(_ animated: Bool) {//將撰寫按鈕弄到最前面//會創建tabBar中的所有控件器對應的按鈕tabBar.bringSubviewToFront(composedButton)}//MARK 懶加載控件private lazy var composedButton: UIButton = { //閉包//自定義樣式的按鈕let button = UIButton()button.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)button.sizeToFit()return button}() } //MARK: -設置界面 extension MainViewController {//設置撰寫按鈕private func setupComposedButton(){tabBar.addSubview(composedButton)// 2 調整按鈕位置let count = children.countprint("children 的數量是 \(count)")//讓按鈕寬一點點 能夠解決手指觸摸容錯的問題// let w = tabBar.bounds.width / CGFloat(count) - 1// composedButton.frame = CGRectInset(tabBar.bounds,2*w,0) // CGRect.insetBy()composedButton.frame = CGRect(x: 128, y: 2, width: composedButton.frame.width, height: composedButton.frame.height)}private func addChildViewControllers(){// 設置tintColor -圖片渲染顏色tabBar.tintColor = UIColor.orangeself.addChildViewController(vc: HomeTableViewController(), title: "首頁", imageName: "tabbar_home")self.addChildViewController(vc: MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")addChild(UIViewController())self.addChildViewController(vc: DiscoverTableViewController(), title: "發現", imageName: "tabbar_discover")self.addChildViewController(vc: ProfileTableViewController(), title: "我", imageName: "tabbar_profile")}private func addChildViewController(vc: UIViewController,title: String,imageName: String){vc.title = titlevc.tabBarItem.image = UIImage(named: imageName)let nav = UINavigationController(rootViewController: vc)addChild(nav)}private func addChildViewController(){let vc = HomeTableViewController()vc.title = "首頁"vc.tabBarItem.image = UIImage(named: "tabbar_home")let nav = UINavigationController(rootViewController: vc)addChild(nav)} }

運行項目,應該可以看到了效果了,項目資源在我的主頁里面。

總結

以上是生活随笔為你收集整理的Swift5.x使用纯代码创建NavigationTab控制器设置启动图Wb第1部分的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。