が良く、私は一緒に以下を入れている事を説明します。これは、サブビューのレイアウトを修正する2つの方法を示しています。制約を使用する場合は、配列として制約を作成し、createredSquareWithConstraintsのコードに示すように、すべてを一度に有効にすることをお勧めします。制約は、1つのビューのフィーチャを別のフィーチャのフィーチャに関連付ける単純な線形方程式です。たとえば、「擬似コード」では、配列の最初の制約は次のように書くことができます。
「サブビューの先頭の余白をコンテナビューの先頭に1を加えたものに0を加えたものに設定します。
(あなたはサブビューの一つの特徴に基づい含むビューの制約を設定したかのように、それは私には見えたとして私は以前に混乱して理由です。)
それはレイアウトを使用するために完全に有効なままであるが私が思うのは、viewWillTransitionToSize()デリゲートメソッドをオーバーライドすることです。ビューメソッドのサブビューのフレームは、含まれているビューのサイズを指定するだけです。そのため、これも実装しました。最初のフレームのある黄色の四角形を作成し、viewWillTransitionToSizeが呼び出されるたびに変更されます。私は個人的には、レイアウトの制約を使用するよりも、あまり控え目に感じることがありません。
ボタンで囲み、画面を回転させると、いずれの方法も同じことが実現するはずです。 [NB私は1つの広場を拘束されたものとして、そして1つは拘束されていないものとしてラベル付けしましたが、実際には両方とも異なる方法で拘束されています。これは明らかに実際に何をするかではないことを付け加えておきます。一つの方法論を選択し、そうでなければあなたのコードを全部置くべきです!
希望に役立ちます!
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開発者には比較的新しいです。リル '助け?ありがとう! –