發表文章

目前顯示的是 6月, 2020的文章

[ iOS / Swift ] 如何更改導覽列 (Navigation Bar) 的返回按鈕 (backBarButtonItem) 標題與圖片

圖片
使用導覽控制器 ( NavigationController ) 來管理畫面,畫面上方預設會有一個導覽列 (Navigation Bar),在 push 到下一個畫面時,都會自帶一個返回按鈕,這個返回按鈕的標題會根據你前個一畫面的標題來設定,我們也可以自己去更改返回按鈕的標題與圖片。 實作: 1. 更改返回按鈕 (backBarButtonItem) 標題: 要在下一個畫面的返回按鈕顯示更改後的標題,需要在前一個畫面就先設定好 barButtonItem 的標題,然後再設定給 navigationItem 的 backBarButtonItem。 override func viewDidLoad () {     super . viewDidLoad ()               navigationItem . title = "測試專案"               let barButtonItem = UIBarButtonItem (title: "返回" , style: . plain , target: self , action: nil )      navigationItem . backBarButtonItem = barButtonItem } 成果: 2. 更改返回按鈕 (backBarButtonItem) 圖片: 使用  UINavigationBarAppearance  類別來設置 Navigation Bar 的外觀,先產生 UINavigationBarAppearance 物件 barAppearance。 設定 barAppearance 的 setBackIndicatorImage,把 backIndicatorImage 和 transitionMaskImage 都設為一樣的圖片。 將 barAppearance 設為目前畫面 navigationBar 的 standardAppearance。 override func viewDidLoad () {     super . viewDidLoad ()          navigationItem . title = "測試專案"       

[ iOS / Swift ] 使用 Swift 4.2 新增的 random 方法來隨機生成顏色,並使用 Extension 擴展 UIColor

圖片
蘋果在 Swift 4.2 時新增了 Random Unification ,讓我們大家在使用亂數的功能時能更加的方便。 實作: 1. 可以使用 Extension 來擴展 UIColor,讓其它畫面都可以呼叫到。 2. 宣告一個 static 變數。 3. UIColor 初始化時需要帶入的是 CGFloat,所以使用 CGFloat.random 來隨機生成 0...1 之間的值,並帶入 UIColor 初始化生成顏色。 public init (red: CGFloat , green: CGFloat , blue: CGFloat , alpha: CGFloat ) extension UIColor {          static var randomColor : UIColor {         let red = CGFloat . random (in: 0 ... 1 )         let green = CGFloat . random (in: 0 ... 1 )         let blue = CGFloat . random (in: 0 ... 1 )         return UIColor (red: red, green: green, blue: blue, alpha: 1.0 )     } }      cell. imageView . backgroundColor = . randomColor 成果:

[ iOS / Swift ] 使用 iOS 13 新增的 UINavigationBarAppearance 類別來設置 Navigation Bar 透明外觀

圖片
蘋果在 iOS 13 新增了 UINavigationBarAppearance  類別,讓我們大家在設置 Navigation Bar 的外觀更加的方便。 問題: 如何讓 (圖一) 畫面 push 到 (圖二) 畫面時, 讓 (圖二) 畫面的 Navigation Bar 變成透明。 實作: 1. 在 (圖二) 畫面要顯示時,產生 UINavigationBarAppearance 物件後,呼叫 configureWithTransparentBackground() 方法,將背景和陰影屬性重置為透明,然後在設為目前畫面 NavigationBar 的 standardAppearance。 2. 在 (圖二) 畫面要消失時,呼叫 configureWithDefaultBackground() 方法,將背景和陰影屬性重置為預設。      override func viewWillAppear ( _ animated: Bool ) {      super . viewWillAppear (animated)               let navigationBarAppearance = UINavigationBarAppearance ()     navigationBarAppearance. configureWithTransparentBackground ()      navigationController ?. navigationBar . standardAppearance = navigationBarAppearance }      override func viewWillDisappear ( _ animated: Bool ) {      super . viewWillDisappear (animated)              let navigationBarAppearance = UINavigationBarAppearance ()     navigationBarAppearance. configureWithDefaultBackground ()      navigationController ?. navigationB

[ iOS / Swift ] 解決 Navigation Bar 設為不半透明, push 後再返回會出現 UICollectionView、UITableView 被 Navigation Bar 遮住的問題

圖片
遇到的問題: 在上次解決 Navigation Bar 設為 不半透明 ,添加 UISearchBar  後 push 會出現間距。 [ 前往文章 ]  又出現新的問題,(圖ㄧ) 畫面 push 到 (圖二) 畫面後再返回 (圖ㄧ) 畫面後,會出現 UICollectionView、UITableView 會被 Navigation Bar 遮住。 解決的方法: 在 (圖一) 畫面將要顯示時加入對 navigationController view 重新佈局的方法。 override func viewWillAppear ( _ animated: Bool ) {      super . viewWillAppear (animated)              navigationController ?. view . setNeedsLayout ()     navigationController ?. view . layoutIfNeeded () } 解決後成果: Navigation Bar 在設為 不半透明 時,(圖ㄧ) 畫面 push 到 (圖二) 畫面後再返回 (圖ㄧ) 畫面後,不會再出現 UICollectionView、UITableView 會被 Navigation Bar 遮住的情況發生。 修正 Bug: 後來發現在  (圖ㄧ) 畫面 push 到 (圖二)  畫面時,會出現間距。這是  Navigation Bar 設為 不半透明 才會出現的問題。 後來經過查詢後發現只要在 viewDidLoad 加入  extendedLayoutIncludesOpaqueBars  設為 true ,讓 View 可以在 Navigation Bar 設為 不半透明 時,可以擴展佈局到 Navigation Bar 底部,就可以解決這個佈局的 Bug。 override   func   viewDidLoad () {     super . viewDidLoad ()      extendedLayoutIncludesOpaqueBars  =  true } //    override func viewWillAppear(_ animated: Bool) { //         super.vi

[ iOS / Swift ] 解決 Navigation Bar 設為不半透明,添加 UISearchBar 後 push 會出現間距

圖片
遇到的問題: Navigation Bar 在設為 不半透明 時,會出現一些佈局的問題, (圖一) 畫面 push 到 (圖二) 畫面時會多出 12 px 的間距。因為 (圖一) 畫面的 Navigation Bar 在添加 UISearchBar 後的高度為 56 px,比原本高度 44 px 多出了 12 px,導致在 push 到 (圖二) 畫面時會多出 12 px 的間距。 解決的方法: 在 (圖一) 畫面將要消失時加入對 navigationController view 重新佈局的方法。 override func viewWillDisappear ( _ animated: Bool ) {      super . viewWillDisappear (animated)               navigationController ?. view . setNeedsLayout ()      navigationController ?. view . layoutIfNeeded () } 解決後成果: Navigation Bar 在設為 不半透明 時, (圖ㄧ)  畫面 push 到 (圖二) 畫面後, (圖二) 畫面不會在多出 12 px 的間距。