Project 03 - View

DaY·2021년 3월 20일
1

iOS

목록 보기
12/52
post-thumbnail

main.storyboard를 이용해 View를 구성하는 것은 제일 쉬운 방법이다.
하지만 이 방법 외에도 코드를 이용해 View를 구성할 수 있다.

private let termsButton: UIButton = {
    let button = UIButton()
    button.setTitle("Terms of Service", for: .normal)
    button.setTitleColor(.secondaryLabel, for: .normal)
    return button
}()

위와 같이 먼저 넣고 싶은 View의 설정을 해주고 viewDidLoad() 함수 내에서 SubView로 추가해주면 된다.

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(termsButton)
}

위치를 지정해주고 싶으면 layoutSubviews를 override 해준다.

override func layoutSubviews() {
    super.layoutSubviews()
        
    profilePhotoImageView.frame = CGRect(x: 5,
                                             y: 5,
                                             width: profilePhotoSize,
                                             height: profilePhotoSize).integral

    postsButton.frame = CGRect(x: profilePhotoImageView.right,
                                   y: 5,
                                   width: countButtonWidth,
                                   height: buttonHeight).integral
}

위와 같이 CGRect를 사용해 x, y 좌표를 지정하고 width 와 height를 지정해주어야 한다.

또한 색상, 글자 크기, 글자 폰트 등의 이름을 지정하여 사용할 수 있다.

import UIKit

public struct Specs {
    public struct Color {
        public let tint = UIColor(red: 0.77, green: 0.82, blue: 0.86, alpha: 1.00)
        public let red = UIColor.red
        public let blue = UIColor(hex: 0x228aae)
    }
    
    public static var color: Color {
        return Color()
    }
}

위와 같이 지정된 색상들을 아래와 같이 불러와 쉽게 사용 가능하다.

navigationController?.navigationBar.barTintColor = Specs.color.tint

0개의 댓글