1. 程式人生 > >[iOS開發]滑動時狀態列和內容重合

[iOS開發]滑動時狀態列和內容重合

問題

這裡指,當將內容向上滑動時文字與狀態列重合而非一開始就重合
像這樣:
問題

解決

  1. 使用導航欄,或
  2. 新增背景圖,或
  3. 模糊狀態列。

使用導航欄

StoryBoard中選中當前Controller,選擇Editor-> Embed in-> Navigation Controller。

模糊狀態列

程式碼實現如下:

let statusBarHeight = UIApplication.shared.statusBarFrame.height
let blur = UIBlurEffect(style: .regular)
let blurStatusBar = UIVisualEffectView(frame: CGRect(x
: 0, y: 0, width: view.bounds.width, height: statusBarHeight)) blurStatusBar.effect = blur view.addSubview(blurStatusBar)

我在這裡犯了一個錯誤:在我的程式碼裡,這個Scene其實是一個UITableViewController,因此view就是UITableViewControllerView。如此一來,新增的模糊就會隨著Table的滑動而滑動。
我最終這樣解決:不使用UITableViewController,而是把UITableView放在UIViewController中。

總結

背景圖和模糊狀態列雖然能起到預期效果,但是總覺得不好看。個人喜歡使用導航欄,再加上iOS11中的Prefers Large Titles效果很棒,就更喜歡了。

2018.07.14