第七章 美化DetailView界面

本項目是《beginning iOS8 programming with swift》中的項目學習筆記==》所有筆記目錄html

------------------------------------------------------------------------------------------------------------------swift

1. 往DetailViewController裏拖一個Tabel View控件,設置prototype cells爲1,將imageView控件在文檔視圖中拖到Table View裏面,做爲Header,設置表格的數據源和代理。
2. Aspect Fit等圖片設置原理以下圖:
app

 

 

 

 

3. 往原型Cell裏拖入兩個Label控件,分別設置文字爲Name和Value。並新建一個自定義UITableViewCell類DetailTableViewCell,並定義兩個成員變量,關聯到控件上。
4. 修改DetailViewController中的成員變量爲Restaurant,實現表格數據源方法(貌似這裏不用連線tableView到IBOutlet):
ide

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("DetailTableCell", forIndexPath: indexPath) as DetailTableViewCell
    switch indexPath.row {
    case 0:
        cell.nameLabel.text = "Name"
        cell.valueLabel.text = restaurant.name
    case 1:
        cell.nameLabel.text = "Type"
        cell.valueLabel.text = restaurant.type
    case 2:
        cell.nameLabel.text = "Location"
        cell.valueLabel.text = restaurant.location
    case 3:
        cell.nameLabel.text = "Been here"
        cell.valueLabel.text = restaurant.isVisited ? "I've been here" : "No"
    default:
        cell.nameLabel.text = ""
        cell.valueLabel.text = ""
    }
 
         // 設置單元格背景色
    cell.backgroundColor = UIColor.clearColor()
    // 清除多餘的表格行
    tableView.tableFooterView = UIView(frame: CGRectZero)
    // 自定義單元格分割線
    tableView.separatorColor = UIColor.orangeColor()
   
    return cell
}

效果圖:學習

 5. 自定義導航欄主題
didFinishLaunchingWithOptions方法中自定義導航欄,也能夠自定義一個導航控制器類,並在initialize方法中設置導航欄主題字體

// 設置導航欄背景色
UINavigationBar.appearance().barTintColor = UIColor.orangeColor()
// 設置導航欄上面Item的顏色
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
// 設置導航欄文字字體
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(22.0)]

6. 設置返回按鈕文字爲空(viewDidLoad):spa

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)

7. 設置導航欄標題(Detail的viewDidLoad):prototype

self.title = restaurant.name

8. 設置滑動隱藏導航欄。能夠在storyboard中設置OnSwipe勾上,可是這樣會對全局的導航欄生效,因此若是隻想Restaurant表格滑動隱藏導航欄而Detail表格滑動不隱藏,最好是在代碼裏設置。能夠在哪兒設置合適呢?viewDidLoad中嗎?若是在Restaurant中設置true,在Detail中設置false,可是導航後並不能按照預期的變化。由於viewDidLoad方法只會在界面初次加載的時候執行一次。所以,在Restaurant控制器和Detail控制器的viewWillAppear方法中分別執行:代理

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
       
    self.navigationController?.hidesBarsOnSwipe = true
}
 
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
       
    self.navigationController?.hidesBarsOnSwipe = false
    self.navigationController?.setNavigationBarHidden(false, animated: true)
} 

9. 如今導航欄文字顏色是白色,可是狀態欄顏色是黑色,須要處理一下。
  9.1 若是是修改一個控制器的狀態欄:rest

override func preferredStatusBarStyle() ->UIStatusBarStyle {
    return .LightContent
}

 9.2 若是是修改整個應用程序的狀態欄:
    1. 修改View controller-based status bar appearance設置爲NO

  2. 在didFinishLaunchingWithOptions方法中:
UIApplication.sharedApplication().statusBarStyle = .LightContent  

相關文章
相關標籤/搜索