Skip to content

Instantly share code, notes, and snippets.

@wiwi-git
Created June 17, 2021 14:05
Show Gist options
  • Save wiwi-git/12fa043a52c2fb3a65857b8e488873d8 to your computer and use it in GitHub Desktop.
Save wiwi-git/12fa043a52c2fb3a65857b8e488873d8 to your computer and use it in GitHub Desktop.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
var data = [
"AB",
"BC",
"CD",
"DE",
"EF"
]
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.text = "Hello ~ PickerView"
label.textColor = .black
label.textAlignment = .center
view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: view.topAnchor, constant: 40),
label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
label.heightAnchor.constraint(equalToConstant: 30)
])
let pv = UIPickerView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
pv.delegate = self
pv.dataSource = self
pv.layer.borderWidth = 1
pv.layer.borderColor = UIColor.black.cgColor
view.addSubview(pv)
pv.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
pv.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 20),
pv.centerXAnchor.constraint(equalTo: view.centerXAnchor),
pv.widthAnchor.constraint(equalToConstant: 80),
pv.heightAnchor.constraint(equalToConstant: 80)
])
self.view = view
print("--")
self.view = view
}
}
extension MyViewController: UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.data.count
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return NSAttributedString(string: self.data[row], attributes: [.foregroundColor:UIColor.red])
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment