기본적으로 스택뷰 안에 컴포넌트들은 다 꽉채워진다. 그래서 띄워주려면 UIView 넣고 그안에 넣는 방법이 있음!
//
// SymbolRollerViewController.swift
// SymbolRoller
//
//
import UIKit
class SymbolRollerViewController: UIViewController {
let symbols: [String] = ["sun.min", "moon", "cloud", "wind", "snowflake"]
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
print("view did load")
//뷰에 필요한 컴포넌트들이 메모리에 로드되는 시점, 화면에 뜨기전에 준비하는 시점
button.layer.cornerRadius = 40
loadRandomSymbol()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//화면에 보여질 시점
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//화면에 보여지고 난 시점
}
@IBAction func buttonTapped(_ sender: Any) {
loadRandomSymbol()
}
//TO-DO
//심볼에서 하나를 임의로 추출해서 이미지와 텍스트를 설정한다.
func loadRandomSymbol() {
let symbol = symbols.randomElement()!
imageView.image = UIImage(systemName: symbol)
label.text = symbol
}
}
날씨 화면 구현
//
// WeatherViewController.swift
// SimpleWeather
//
//
import UIKit
class WeatherViewController: UIViewController {
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var weatherImageView: UIImageView!
@IBOutlet weak var temperatureLabel: UILabel!
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button.layer.cornerRadius = 40
// Do any additional setup after loading the view.
}
let cities = ["Seoul", "Tokyo", "LA", "Seattle"]
let weathers = ["cloud.fill", "sun.max.fill", "wind", "cloud.sun.rain.fill"]
@IBAction func changeButtonTapped(_ sender: Any) {
cityLabel.text = cities.randomElement()
let abc = weathers.randomElement()!
weatherImageView.image = UIImage(systemName: abc)?.withRenderingMode(.alwaysOriginal)
temperatureLabel.text = "\(Int.random(in: 10..<30))"
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}