하는 것이 더 나은 내가 다음 조립 한 것을 설명하기. 이 방법은 하위 뷰 레이아웃을 수정하는 두 가지 방법을 보여줍니다. 제약 조건을 사용할 때, 제약 조건을 배열로 생성하고 한 번에 모두 활성화하는 것을 선호합니다. createredSquareWithConstraints 코드에서 볼 수 있습니다. 제약 조건은 한 관점의 특징을 다른 관점의 특성과 관련시키는 단순한 선형 방정식입니다. 예를 들어 "pseudocode"에서 배열의 첫 번째 제약 조건은 다음과 같이 쓸 수 있습니다.
"컨테이너 뷰의 선행 마진의 1 배에 0을 더한 값과 동일한 서브 뷰의 선행 마진을 설정합니다.
(당신은 그것의 파단 중 하나의 특성에 따라 포함하는 뷰의 제약 조건을 설정하는 것처럼 그것이 나에게 보였다 나는 이전에 혼란스러워지고 한 이유입니다.)
이 레이아웃을 사용하는 것이 완벽하게 유효하지만 제약 조건, 나는 선호하는 방법론 요즘은 viewWillTransitionToSize() delegate 메소드를 오버라이드하는 것으로 생각합니다.이 메소드는 포함 된 뷰의 크기를 지정하면 뷰 컨트롤러의 서브 뷰의 프레임을 지정해야합니다. 따라서, 이것도 구현해 보았습니다. 초기 프레임이있는 노란색 사각형을 만들고 viewWillTransitionToSize가 호출 될 때마다 수정됩니다. 필자는 개인적으로 레이아웃 제약 조건을 사용하는 것보다 훨씬 덜 까다롭게 느껴진다.
버튼으로 배치하고 화면을 회전하면 두 방법 모두 동일한 결과가 나타납니다. [주의 : 나는 하나의 사각형을 제약 된 것으로 표시하고 하나는 제약되지 않은 것으로 표시했다. 그러나 현실적으로 그들은 모두 다른 방식으로 제한되어있다. 나는 이것이 당신이 실제로 어떻게 할 것인가는 분명하지 않다는 것을 추가 할 것입니다 - 당신은 하나의 방법론을 선택해야하고 그렇지 않으면 당신의 코드는 모든 곳에서있을 것입니다!].
희망 하시겠습니까?
import UIKit
class ViewController: UIViewController {
var constrainedredSquare : UIView!
var unconstrainedRedSquare : UIView!
var methodOneButton : UIButton!
var methodTwoButton : UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.blue
func getButton(name: String) -> UIButton {
let button : UIButton = UIButton()
button.backgroundColor = UIColor.white
button.layer.cornerRadius = 3
button.clipsToBounds = true
button.setTitle(name, for: UIControlState.normal)
button.setTitleColor(UIColor.black, for: UIControlState.normal)
return button
}
self.methodOneButton = getButton(name: "Red - Constraints")
self.methodTwoButton = getButton(name: "Yellow - viewWillTransitionToSize")
self.methodOneButton.addTarget(self, action: #selector(self.createRedSquareWithConstraints), for: .touchUpInside)
self.methodTwoButton.addTarget(self, action: #selector(self.createYellowSquareWithoutConstraints), for: .touchUpInside)
self.methodOneButton.frame = CGRect(origin: CGPoint(x: 200, y: 100), size: CGSize(width: 300, height: 300))
self.methodTwoButton.frame = CGRect(origin: CGPoint(x: self.view.frame.width - 500, y: 100), size: CGSize(width: 300, height: 300))
self.view.addSubview(self.methodOneButton)
self.view.addSubview(self.methodTwoButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if let _ = self.unconstrainedRedSquare {
self.unconstrainedRedSquare.frame = CGRect(origin: CGPoint.zero, size: size)
}
self.methodOneButton.frame = CGRect(origin: CGPoint(x: 200, y: 100), size: CGSize(width: 300, height: 300))
self.methodTwoButton.frame = CGRect(origin: CGPoint(x: size.width - 500, y: 100), size: CGSize(width: 300, height: 300))
}
func createYellowSquareWithoutConstraints() {
if let _ = self.unconstrainedRedSquare {
self.unconstrainedRedSquare.removeFromSuperview()
}
else
{
if let _ = constrainedredSquare {
self.constrainedredSquare.removeFromSuperview()
}
self.unconstrainedRedSquare = UIView()
self.unconstrainedRedSquare.backgroundColor = UIColor.yellow
self.unconstrainedRedSquare.frame = CGRect(origin: CGPoint.zero, size: self.view.frame.size)
self.view.addSubview(self.unconstrainedRedSquare)
self.view.bringSubview(toFront: self.methodOneButton)
self.view.bringSubview(toFront: self.methodTwoButton)
}
}
func createRedSquareWithConstraints() {
if let _ = self.constrainedredSquare {
self.constrainedredSquare.removeFromSuperview()
}
else
{
if let _ = self.unconstrainedRedSquare {
self.unconstrainedRedSquare.removeFromSuperview()
}
let redSquare : UIView = UIView()
redSquare.backgroundColor = UIColor.red
self.view.addSubview(redSquare)
self.view.bringSubview(toFront: self.methodOneButton)
self.view.bringSubview(toFront: self.methodTwoButton)
let rsConstraints : [NSLayoutConstraint] = [NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.leading, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.trailing, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: redSquare, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.height, multiplier: 1.0, constant: 0)]
redSquare.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(rsConstraints)
}
}
}
이 바보 같은 질문이 될 수있다/내가 제대로 코드를 읽는되지 않을 수 있습니다하지만 당신이 귀하의 제약 조건을 잘못 잡았습니까? 이전 뷰에 대한 제약 조건을 기반으로 새 뷰에 대한 제약 조건을 설정하지 않아야합니까? 새로운 자식 컨트롤러의 뷰를 기반으로 부모 컨트롤러의 뷰에 대한 제약 조건을 설정하는 것처럼 보입니다. – Sparky
@ Sparky 이런 뜻인가요? controller.view.topAnchor.constraint (equalTo : view.topAnchor) .isActive = true입니다. 나는 그것을했고 그 행동은 똑같다. –
@ Sparky? 죄송합니다, 저는 iOS 개발자에게 상대적으로 새로운입니다. 릴 도와 줘? 감사! –