뷰를 만드는 방법과 컨텐트를 표시하는 방법, 아울렛 없이 뷰에 접근하는 방법, 이벤트 처리를 제어하는 방법, 뷰가 제공하는 시각적인 속성들에서 알아보도록 하겠습니다.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let frame = CGRect(x: 50, y: 50, width: 100, height: 100)
let frame2 = CGRect(x: 100, y: 100, width: 100, height: 100)
let frame3 = CGRect(x: 150, y: 400, width: 200, height: 200)
let v = UIView(frame: frame)
let v2 = UIView(frame: frame2)
let v3 = UIView(frame: frame3)
view.addSubview(v)
view.addSubview(v2)
view.addSubview(v3)
v.backgroundColor = UIColor.systemRed
v2.backgroundColor = UIColor.blue
v3.backgroundColor = UIColor.black
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var modeLabel: UILabel!
@IBAction func switchButton(_ sender: Any) {
let currentMode = imageView.contentMode.rawValue
let newMode = UIView.ContentMode(rawValue: currentMode + 1) ?? .scaleAspectFill
imageView.contentMode = newMode
updateModeLabel()
}
func updateModeLabel() {
switch imageView.contentMode {
case .scaleToFill:
modeLabel.text = "Scale to fill"
case .scaleAspectFit:
modeLabel.text = "Aspect fit"
case .scaleAspectFill:
modeLabel.text = "Aspect fill"
case .redraw:
modeLabel.text = "Redraw"
case .center:
modeLabel.text = "Center"
case .top:
modeLabel.text = "Top"
case .bottom:
modeLabel.text = "Bottom"
case .left:
modeLabel.text = "Left"
case .right:
modeLabel.text = "Right"
case .topLeft:
modeLabel.text = "Top left"
case .topRight:
modeLabel.text = "Top right"
case .bottomLeft:
modeLabel.text = "Bottom left"
case .bottomRight:
modeLabel.text = "Bottom right"
@unknown default:
fatalError()
}
}
override func viewDidLoad() {
super.viewDidLoad()
imageView.layer.borderColor = UIColor.red.cgColor
imageView.layer.borderWidth = 3
updateModeLabel()
}
}
import UIKit
class ViewController: UIViewController {
@IBAction func changeColor(_ sender: Any) {
if let v = view.viewWithTag(10) {
v.backgroundColor = .red
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}