본문 바로가기

Mobile/IOS & Swift

[IOS] Auto layout 코드 예시

[SWIFT] 수동으로 auto layout 잡아주기

스토리보드로 하나하나 드래그해주어도 되지만 가끔은 수동으로 잡아주어야 할 때가 있다.
이럴경우 contentView.translatesAutoresizingMaskIntoConstraints = false처럼
우선 해당부분의 auto Resizing을 false를 통해 코드로 직접 수정 가능하도록 설정해 준 후
하기 소스를 보자

contentView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 8.0),
contentView.leadingAnchor.constraint(equalTo: view.leadingAnchor), //left
logoView.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier:2.0),

첫번째 줄은
ContentView의 위 앵커가 최상위 레이아웃 가이드(topLayoutGuide)의 하단과 매칭시키며 이때 값을 8.0을 줍니다.
두번째줄은
contentView의왼쪽(left)가 view 의 왼쪽과 일치시키며 constant는 0으로 줍니다.
세번째줄은
logoView의 height을 contentView의 height의 1/2배만큼 줍니다.
두번째 인자 * multipier는 첫번째 인자의 값입니다.

예시소스

// auto resizing
// ContentView .
//false Auto Resizing .
contentView.translatesAutoresizingMaskIntoConstraints = false
//view root View
view.addSubview(contentView)
logoView.translatesAutoresizingMaskIntoConstraints = false;
contentView.addSubview(logoView)
buttonView.translatesAutoresizingMaskIntoConstraints = false;
contentView.addSubview(buttonView)
//titles Button
for(index, title) in titles.enumerated(){
let button = RoundedButton()
button.translatesAutoresizingMaskIntoConstraints = false
buttonView.addSubview(button)
button.backgroundColor = UIColor(red: 52/255, green: 152/255, blue: 219/255, alpha: 1.0)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
button.setTitle(title, for:.normal)
button.tag = index
gameButtons.append(button)
}
scoreView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(scoreView)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
recentScoreLabel.translatesAutoresizingMaskIntoConstraints = false
highscoreLabel.translatesAutoresizingMaskIntoConstraints = false
scoreView.addSubview(titleLabel)
scoreView.addSubview(recentScoreLabel)
scoreView.addSubview(highscoreLabel)
titleLabel.textAlignment = .center
titleLabel.font = UIFont.boldSystemFont(ofSize: 30)
titleLabel.textColor = UIColor.white
recentScoreLabel.font = UIFont.boldSystemFont(ofSize: 20)
recentScoreLabel.textColor = UIColor.white
highscoreLabel.font = UIFont.boldSystemFont(ofSize: 20)
highscoreLabel.textColor = UIColor.white
titleLabel.text = "Multiple Choices"
recentScoreLabel.text = "Recent: 0"
highscoreLabel.text = "HighScore: 0"
let constraints = [
//topLayoutGuide
contentView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 8.0),
contentView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
logoView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20.0),
logoView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6),
logoView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
logoView.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 0.2),
buttonView.topAnchor.constraint(equalTo: logoView.bottomAnchor, constant: 20.0),
buttonView.bottomAnchor.constraint(equalTo: scoreView.topAnchor, constant: -20.0),
buttonView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6),
buttonView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
gameButtons[0].topAnchor.constraint(equalTo: buttonView.topAnchor, constant: 8.0),
gameButtons[0].bottomAnchor.constraint(equalTo: gameButtons[1].topAnchor, constant: -8.0),
gameButtons[0].leadingAnchor.constraint(equalTo: buttonView.leadingAnchor),
gameButtons[0].trailingAnchor.constraint(equalTo: buttonView.trailingAnchor),
gameButtons[1].bottomAnchor.constraint(equalTo: gameButtons[2].topAnchor, constant: -8.0),
gameButtons[1].leadingAnchor.constraint(equalTo: buttonView.leadingAnchor),
gameButtons[1].trailingAnchor.constraint(equalTo: buttonView.trailingAnchor),
gameButtons[2].bottomAnchor.constraint(equalTo: gameButtons[3].topAnchor, constant: -8.0),
gameButtons[2].leadingAnchor.constraint(equalTo: buttonView.leadingAnchor),
gameButtons[2].trailingAnchor.constraint(equalTo: buttonView.trailingAnchor),
gameButtons[3].bottomAnchor.constraint(equalTo: buttonView.bottomAnchor, constant: -8.0),
gameButtons[3].leadingAnchor.constraint(equalTo: buttonView.leadingAnchor),
gameButtons[3].trailingAnchor.constraint(equalTo: buttonView.trailingAnchor),
gameButtons[0].heightAnchor.constraint(equalTo: gameButtons[1].heightAnchor),
gameButtons[1].heightAnchor.constraint(equalTo: gameButtons[2].heightAnchor),
gameButtons[2].heightAnchor.constraint(equalTo: gameButtons[3].heightAnchor),
scoreView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -40.0),
scoreView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.6),
scoreView.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: 0.3),
scoreView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
titleLabel.topAnchor.constraint(equalTo: scoreView.topAnchor, constant: 8.0),
titleLabel.leadingAnchor.constraint(equalTo: scoreView.leadingAnchor),
titleLabel.trailingAnchor.constraint(equalTo: scoreView.trailingAnchor),
titleLabel.bottomAnchor.constraint(equalTo: recentScoreLabel.topAnchor, constant: -8.0),
recentScoreLabel.leadingAnchor.constraint(equalTo: scoreView.leadingAnchor),
recentScoreLabel.trailingAnchor.constraint(equalTo: scoreView.trailingAnchor),
recentScoreLabel.bottomAnchor.constraint(equalTo: highscoreLabel.topAnchor, constant: -8.0),
highscoreLabel.leadingAnchor.constraint(equalTo: scoreView.leadingAnchor),
highscoreLabel.trailingAnchor.constraint(equalTo: scoreView.trailingAnchor),
highscoreLabel.bottomAnchor.constraint(equalTo: scoreView.bottomAnchor, constant: -8.0),
titleLabel.heightAnchor.constraint(equalTo: recentScoreLabel.heightAnchor),
recentScoreLabel.heightAnchor.constraint(equalTo: highscoreLabel.heightAnchor)
]
NSLayoutConstraint.activate(constraints)
}