美股記帳 App / Part6. 從 cocoapods 安裝套件來製作 sidemenu 和 寫數學公式
Published in
7 min readApr 5, 2022
安裝 cocoapods 和想要的 package
- 打開 terminal 輸入下面的程式 (只在第一 download 需要)
sudo gem install cocoapods
2. 在 terminal cd 打開 xcode project
cd Desktop/stockTracker
3. 創一個 Podfile並打開它
pod init
ls
open Podfile
4. 在 termimal 安裝
pod install
5. 打開 xcode project 確認 Pods 檔案有沒有生成
ls
open stockTracker.xcworkspace/
製作 sideMenu
- 從 cocoapods import SideMenu
2. 在想要的 controller 加入 SideMenu
import SideMenu
var sideMenu : SideMenuNavigationController?
override func viewDidLoad() {
super.viewDidLoad() // sidemenu setting
// 等等幫 sidemenu 做一個 tableView,先把它設為 rootViewController
sideMenu = SideMenuNavigationController(rootViewController: sideMenuTableViewController()) // 讓sidemuenu 靠左
sideMenu?.leftSide = true
// 加入往左滑可以得到 sidemenu 的手勢 SideMenuManager.default.leftMenuNavigationController = sideMenu
SideMenuManager.default.addPanGestureToPresent(toView: self.view)
}
3. 為 sidemenu 製作一個 tableViewController
- 用 didSelectRowAt function 將使用者倒到想要的頁面
import UIKitclass sideMenuTableViewController: UITableViewController {
let contentList = ["Definition"]override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "sideMenuTableViewCell")
tableView.separatorStyle = .singleLine
tableView.separatorColor = .lightGray
tableView.backgroundColor = .darkGray}// MARK: - Table view data sourceoverride func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contentList.count
}override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "sideMenuTableViewCell", for: indexPath)
var content = cell.defaultContentConfiguration()
content.text = contentList[indexPath.row]
content.textProperties.color = .white
cell.contentConfiguration = content
cell.selectionStyle = .none
cell.backgroundColor = .darkGrayreturn cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0{
guard let controller =
UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "definitionViewController") as? definitionViewController else {
print("fail")
return }
present(controller, animated: true, completion: nil)
}
}
}
下面有詳細的教學可以參考喔
import iosMath 寫美麗的數學公式
- 從 cocoapods 安裝 iosMath 後
- 在 StoryBoard 拉一個 view 並把 class 改成 MTMathUILabel
3. 像編輯 Latex 一樣編輯 formula
- 跳下一行 :\\\\
- 在每一個數學 function 之前加 \\
import iosMath
avgDollarCostMathLabel.latex = "Amount = price \\times shares \\\\ \\text{Average Dollar Cost} = \\frac{\\sum_{i=1}^N Amount_i}{\\text{Number of Shares}} "
下面有官方範例教學喔