[SWIFT] 수동으로 auto layout 잡아주기
스토리보드로 하나하나 드래그해주어도 되지만 가끔은 수동으로 잡아주어야 할 때가 있다.
이럴경우 contentView.translatesAutoresizingMaskIntoConstraints = false처럼
우선 해당부분의 auto Resizing을 false를 통해 코드로 직접 수정 가능하도록 설정해 준 후
하기 소스를 보자
contentView.topAnchor.constraint(: topLayoutGuide.bottomAnchor, : 8.0),contentView.leadingAnchor.constraint(: view.leadingAnchor), //leftlogoView.heightAnchor.constraint(: contentView.heightAnchor, :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 Viewview.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 = falsebuttonView.addSubview(button)button.backgroundColor = UIColor(: 52/255, : 152/255, : 219/255, : 1.0)button.titleLabel?.font = UIFont.boldSystemFont(: 20)button.setTitle(title, :.normal)button.tag = indexgameButtons.append(button)}scoreView.translatesAutoresizingMaskIntoConstraints = falsecontentView.addSubview(scoreView)titleLabel.translatesAutoresizingMaskIntoConstraints = falserecentScoreLabel.translatesAutoresizingMaskIntoConstraints = falsehighscoreLabel.translatesAutoresizingMaskIntoConstraints = falsescoreView.addSubview(titleLabel)scoreView.addSubview(recentScoreLabel)scoreView.addSubview(highscoreLabel)titleLabel.textAlignment = .centertitleLabel.font = UIFont.boldSystemFont(: 30)titleLabel.textColor = UIColor.whiterecentScoreLabel.font = UIFont.boldSystemFont(: 20)recentScoreLabel.textColor = UIColor.whitehighscoreLabel.font = UIFont.boldSystemFont(: 20)highscoreLabel.textColor = UIColor.whitetitleLabel.text = "Multiple Choices"recentScoreLabel.text = "Recent: 0"highscoreLabel.text = "HighScore: 0"let constraints = [//topLayoutGuide는 부모 레이아웃을 가리킨다contentView.topAnchor.constraint(: topLayoutGuide.bottomAnchor, : 8.0),contentView.leadingAnchor.constraint(: view.leadingAnchor),contentView.trailingAnchor.constraint(: view.trailingAnchor),contentView.bottomAnchor.constraint(: view.bottomAnchor),logoView.topAnchor.constraint(: contentView.topAnchor, : 20.0),logoView.widthAnchor.constraint(: contentView.widthAnchor, : 0.6),logoView.centerXAnchor.constraint(: contentView.centerXAnchor),logoView.heightAnchor.constraint(: contentView.heightAnchor, : 0.2),buttonView.topAnchor.constraint(: logoView.bottomAnchor, : 20.0),buttonView.bottomAnchor.constraint(: scoreView.topAnchor, : -20.0),buttonView.widthAnchor.constraint(: contentView.widthAnchor, : 0.6),buttonView.centerXAnchor.constraint(: contentView.centerXAnchor),gameButtons[0].topAnchor.constraint(: buttonView.topAnchor, : 8.0),gameButtons[0].bottomAnchor.constraint(: gameButtons[1].topAnchor, : -8.0),gameButtons[0].leadingAnchor.constraint(: buttonView.leadingAnchor),gameButtons[0].trailingAnchor.constraint(: buttonView.trailingAnchor),gameButtons[1].bottomAnchor.constraint(: gameButtons[2].topAnchor, : -8.0),gameButtons[1].leadingAnchor.constraint(: buttonView.leadingAnchor),gameButtons[1].trailingAnchor.constraint(: buttonView.trailingAnchor),gameButtons[2].bottomAnchor.constraint(: gameButtons[3].topAnchor, : -8.0),gameButtons[2].leadingAnchor.constraint(: buttonView.leadingAnchor),gameButtons[2].trailingAnchor.constraint(: buttonView.trailingAnchor),gameButtons[3].bottomAnchor.constraint(: buttonView.bottomAnchor, : -8.0),gameButtons[3].leadingAnchor.constraint(: buttonView.leadingAnchor),gameButtons[3].trailingAnchor.constraint(: buttonView.trailingAnchor),gameButtons[0].heightAnchor.constraint(: gameButtons[1].heightAnchor),gameButtons[1].heightAnchor.constraint(: gameButtons[2].heightAnchor),gameButtons[2].heightAnchor.constraint(: gameButtons[3].heightAnchor),scoreView.bottomAnchor.constraint(: contentView.bottomAnchor, : -40.0),scoreView.widthAnchor.constraint(: contentView.widthAnchor, : 0.6),scoreView.heightAnchor.constraint(: contentView.heightAnchor, : 0.3),scoreView.centerXAnchor.constraint(: contentView.centerXAnchor),titleLabel.topAnchor.constraint(: scoreView.topAnchor, : 8.0),titleLabel.leadingAnchor.constraint(: scoreView.leadingAnchor),titleLabel.trailingAnchor.constraint(: scoreView.trailingAnchor),titleLabel.bottomAnchor.constraint(: recentScoreLabel.topAnchor, : -8.0),recentScoreLabel.leadingAnchor.constraint(: scoreView.leadingAnchor),recentScoreLabel.trailingAnchor.constraint(: scoreView.trailingAnchor),recentScoreLabel.bottomAnchor.constraint(: highscoreLabel.topAnchor, : -8.0),highscoreLabel.leadingAnchor.constraint(: scoreView.leadingAnchor),highscoreLabel.trailingAnchor.constraint(: scoreView.trailingAnchor),highscoreLabel.bottomAnchor.constraint(: scoreView.bottomAnchor, : -8.0),titleLabel.heightAnchor.constraint(: recentScoreLabel.heightAnchor),recentScoreLabel.heightAnchor.constraint(: highscoreLabel.heightAnchor)]NSLayoutConstraint.activate(constraints)}