UIKit UI 元件常用的 property & method

UILabel,UITextField,UITextView

利用 text 存取文字,型別為 String?。

print(label.text!)
label.text = "一日不見,如三秋兮"

UIImageView

利用 image 存取圖片,型別為 UIImage?。

imageView.image = UIImage(named: "peter")

UISlider,UIStepper

利用 value 存取數值,型別為 Float,範圍由 maximumValue & minimumValue 控制。

slider.maximumValue = 100
slider.minimumValue = 0
print(slider.value)
slider.value = 50

stepper 還可設定 Step,控制每次按 + / — 時增減的數量。

UISwitch

利用 isOn 存取目前開關是否打開,型別為 Bool。

let airplaneModeSwitch = UISwitch()
print(airplaneModeSwitch.isOn)
airplaneModeSwitch.isOn = true

UISegmentedControl

利用 selectedSegmentIndex 存取目前選到區塊的編號,型別為 Int。

UISegmentedControl 裡每個區塊對應一個數字,數字從 0 開始,從左由右遞增。我們可從 selectedSegmentIndex 得到目前選到區塊的數字,比方數字 2 表示選到第三個區塊。

print(segmentedControl.selectedSegmentIndex)
segmentedControl.selectedSegmentIndex = 1

利用 titleForSegment(at:) 得到標題,比方從 segmentedControl.titleForSegment(at: 0) 得到第一個區塊的標題,從 segmentedControl.titleForSegment(at: segmentedControl.selectedSegmentIndex) 得到目前選到區塊的標題。

print(segmentedControl.titleForSegment(at: 0)!)
print(segmentedControl.titleForSegment(at: segmentedControl.selectedSegmentIndex)!)

UIDatePicker

利用 date 存取選到的時間,型別為 Date。

let datePicker = UIDatePicker()
print(datePicker.date)
datePicker.date = Date()

UIProgressView

利用 progress 存取進度,型別為 Float,範圍 0 ~ 1。

print(progressView.progress)
progressView.progress = 0.5

UIButton

讀取標題: currentTitle,型別為 String?。

設定標題: 呼叫 function setTitle(_:for:)。

print(button.currentTitle)
button.setTitle("青青子衿,悠悠我心", for: .normal)

UINavigationItem

利用 title 存取 navigation bar 上的標題。

navigationItem.title = "人間處處儘是峰迴路轉"

UIPageControl

利用 currentPage 存取目前在第幾個小圓點,數字從 0 開始,型別為 Int。小圓點的數量則由 numberOfPages 決定。

print(pageControl.currentPage)
pageControl.currentPage = 1
print(pageControl.numberOfPages)

UIActivityIndicatorView

開始轉彩球: startAnimating

停止轉彩球: stopAnimating

activityIndicatorView.startAnimating()
activityIndicatorView.stopAnimating()

UIView

backgroundColor: 背景顏色,型別為 UIColor?。

frame: 位置大小,型別為 CGRect。

alpha: 透明度,型別為 CGFloat,範圍 0 ~ 1。

tag: 用來區分 view 的編號,型別為 Int。

blueView.backgroundColor = UIColor(red: 120/255, green: 0, blue: 0, alpha: 1)
blueView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
blueView.alpha = 0.5
blueView.tag = 3

利用 addSubview 將 redView 加到 blueView 身上。

blueView.addSubview(redView)

利用 removeFromSuperview 移除 redView。

redView.removeFromSuperview()

UIScrollView

contentSize: scroll view 捲動的範圍,型別為 CGSize。

contentOffset: scroll view 目前捲動的位置,型別為 CGPoint。

zoomScale: 縮放的比例,比方 2 表示放大 2 倍。

print(scrollView.contentSize)
print(scrollView.contentOffset)
scrollView.contentOffset = CGPoint(x: 100, y: 0)
scrollView.zoomScale = 2

UITableView

利用 indexPathForSelectedRow 讀取目前選到的 cell 位置,型別為 IndexPath?。

print(tableView.indexPathForSelectedRow)

設定某個位置的 cell 被選取,呼叫 selectRow(at:animated:scrollPosition:)。

tableView.selectRow(at: IndexPath(row: 1, section: 0), animated: true, scrollPosition: .top)

UICollectionView

利用 indexPathsForSelectedItems 讀取目前選到 cell 位置,型別為 [IndexPath]?。

print(collectionView.indexPathsForSelectedItems)

設定某個位置的 cell 被選取,呼叫 selectItem(at:animated:scrollPosition:)。

collectionView.selectItem(at: IndexPath(item: 1, section: 0), animated: true, scrollPosition: .top)

UIPickerView

讀取某個 component 被選到的 row 編號,型別是 Int,component & row 的編號都是從 0 開始。

let row = pickerView.selectedRow(inComponent: 0)

設定某個 component 的某個 row 被選取,呼叫 selectRow(_:inComponent:animated:)。

pickerView.selectRow(1, inComponent: 0, animated: true)

UITabBarController

利用 selectedIndex 存取目前選到 tab 的編號,型別為 Int,編號從 0 開始,另外從 selectedViewController 也可取得目前選到 tab 的 controller。

print(tabBarController.selectedIndex)
print(tabBarController.selectedViewController)
tabBarController.selectedIndex = 1

利用 viewControllers 取得 tab bar 管理的 tab 對應的 view controller,型別為 [UIViewController]?。

print(tabBarController.viewControllers)

--

--

彼得潘的 iOS App Neverland
彼得潘的 Swift iOS App 開發問題解答集

彼得潘的iOS App程式設計入門,文組生的iOS App程式設計入門講師,彼得潘的 Swift 程式設計入門,App程式設計入門作者,http://apppeterpan.strikingly.com