
먼저 기본적인 View에 UIView를 띄워보자!
프로젝트 생성 후 기본적인 View Controller 코드로만 작성해보자
import UIKit
class ViewController: UIViewController {
//subView 를 생성함과 동시에 배경화면과 같은 설정값을 세팅해준다
private let subView : UIView = {
let uiView = UIView()
uiView.backgroundColor = .systemGray
return uiView
}()
//View위에 띄우고 싶은 Layout들은 addSubview를 통해서 등록해준다
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(subView)
configure()
}
//configure 함수를 만들어서 주로 이 안에서 Layout의 내용들을 세팅해줌
func configure(){
}
//주로 Layout 들의 위치와 크기는, viewDidLayoutSubviews 함수를 오버라이딩해서 설정해준다!
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
subView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/2)
}
}
위를 완성했다면 주석과 코드를 보고 이번엔 subView 아래쪽에 Button 하나를 띄워보자!
import UIKit
class ViewController: UIViewController {
//subView 를 생성함과 동시에 배경화면과 같은 설정값을 세팅해준다
private let subView : UIView = {
let uiView = UIView()
uiView.backgroundColor = .systemGray
return uiView
}()
//button 을 생성함과 동시에 배경색과 title 색을 정해준다
let button : UIButton = {
let button = UIButton()
button.backgroundColor = .white
button.setTitleColor(.red, for: .normal)
return button
}()
//View위에 띄우고 싶은 Layout들은 addSubview를 통해서 등록해준다
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(subView)
view.addSubview(button)
configure(with: "Button Title")
}
//configure 함수를 만들어서 주로 이 안에서 Layout의 내용들을 세팅해줌
//외부에서 Title을 입력받아 button의 Title을 설정해줌
func configure(with buttonTitle : String){
button.setTitle(buttonTitle, for: .normal)
}
//주로 Layout 들의 위치와 크기는, viewDidLayoutSubviews 함수를 오버라이딩해서 설정해준다!
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
subView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/2)
button.frame = CGRect(x: view.frame.width/2 - 100, y: view.frame.height/2 + 50, width: 200, height: 100)
}
}
'iOS > UIKit' 카테고리의 다른 글
[UIKit] : CollectionView Header 만들기 - (UICollectionReusableView) (0) | 2022.09.02 |
---|---|
[UIKit] : CollectionView 생성, Cell 등록하기 (0) | 2022.09.02 |
[UIKit] : UIButton 속 이미지 크기 조절하기 (0) | 2022.08.25 |
[UIKit] : 구조체 문자열 원하는 문자열로 바꾸기 (0) | 2022.08.24 |
[UIKit] : Scroll View content 페이지 넘기기 (page Control 사용) (0) | 2022.08.23 |