2017-05-18 4 views
1

私はいくつかの部分的に透明なPNGオーバーレイを使って、ビューにオーバーレイを表示します。オーバーレイの数は、実行時の条件によって異なります。 SwiftにUIImageViewインスタンスを作成してビューに追加したいとします。UIImageView .scaleAspectFitとautolayoutはSwiftからプログラムでは動作しません

私は、ストーリーボードにUIImageViewを追加し、その画像をSwift経由で設定するときに問題が発生することはありません。このような場合、UIImageViewの先頭、末尾、上端、下端を親ラッパーの先頭、末尾、上端、下端に固定します。UIViewUIImageViewの属性インスペクタでは、「コンテンツモード」を「アスペクトフィット」に設定し、スウィフトが画像を設定したときに大きな画像を素早く縮小します。ユーザーは、小さなiPhone画面で画像全体を見ることができます。

UIImageViewを動的に作成し、それを同じ親ラッパーUIViewに追加すると、制約に問題があります。満たすべき制約が多すぎます。

スウィフト経由で作成されたUIImageViewが、ストーリーボードで作成されたUIImageViewと異なる理由がわかりません。

スウィフトで作成したUIImageViewを、ストーリーボードで作成したUIImageViewのように振る舞うにはどうすればよいですか?

スウィートボードで作成したUIImageViewの画像と、Swiftで作成したUIImageViewの画像を設定するスウィフトコードです。

override func viewDidLoad() -> Void { 
    super.viewDidLoad(); 

    let imageFilePath: String = Bundle.main.resourceURL!.appendingPathComponent("img/a.png", isDirectory: false).path; 

    // Use the outlet to set the image in the UIImageView created in the storyboard. 
    // This works. 

    self.storyboardImageView.image = UIImage(contentsOfFile: imageFilePath); 

    // Create a UIImageView programatically and add it to the same wrapper UIView 
    // that also contains the UIImageView above. 
    // This doesn't work. 

    let swiftImageView: UIImageView = UIImageView(image: UIImage(contentsOfFile: imageFilePath)); 

    // The presence of the following line makes no difference 
    // 
    swiftImageView.contentMode = .scaleAspectFill; 

    self.storyboardWrapperView.addSubview(swiftImageView); 

    swiftImageView.topAnchor.constraint(equalTo: self.storyboardWrapperView.topAnchor).isActive = true; 
    swiftImageView.bottomAnchor.constraint(equalTo: self.storyboardWrapperView.bottomAnchor).isActive = true; 
    swiftImageView.leadingAnchor.constraint(equalTo: self.storyboardWrapperView.leadingAnchor).isActive = true; 
    swiftImageView.trailingAnchor.constraint(equalTo: self.storyboardWrapperView.trailingAnchor).isActive = true; 

レイアウトマネージャが競合する制約を満たすようにしているときに、出力がXcodeに表示されます。

2017-05-17 22:45:18.351141-0700 My App[29787:7263069] [LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
     (1) look at each constraint and try to figure out which you don't expect; 
     (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x170096670 h=--& v=--& UIImageView:0x101432d20.midX == 540 (active)>", 
    "<NSLayoutConstraint:0x174092520 H:|-(0)-[UIView:0x101439120] (active, names: '|':UIView:0x10142f4a0)>", 
    "<NSLayoutConstraint:0x174092570 H:[UIView:0x101439120]-(0)-| (active, names: '|':UIView:0x10142f4a0)>", 
    "<NSLayoutConstraint:0x1740936a0 H:|-(0)-[UIImageView:0x101432d20] (active, names: '|':UIView:0x101439120)>", 
    "<NSLayoutConstraint:0x174093790 UIImageView:0x101432d20.trailing == UIView:0x101439120.trailing (active)>", 
    "<NSLayoutConstraint:0x17009b260 'UIView-Encapsulated-Layout-Width' UIView:0x10142f4a0.width == 375 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174093790 UIImageView:0x101432d20.trailing == UIView:0x101439120.trailing (active)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
2017-05-17 22:45:18.360996-0700 My App[29787:7263069] [LayoutConstraints] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
     (1) look at each constraint and try to figure out which you don't expect; 
     (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<_UILayoutSupportConstraint:0x174091cb0 _UILayoutGuide:0x10143e8d0.height == 0 (active)>", 
    "<_UILayoutSupportConstraint:0x174092930 V:|-(0)-[_UILayoutGuide:0x10143e8d0] (active, names: '|':UIView:0x10142f4a0)>", 
    "<_UILayoutSupportConstraint:0x174091da0 _UILayoutGuide:0x10143ddc0.height == 0 (active)>", 
    "<_UILayoutSupportConstraint:0x174091490 _UILayoutGuide:0x10143ddc0.bottom == UIView:0x10142f4a0.bottom (active)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x17009cc50 h=--& v=--& UIImageView:0x101432d20.midY == 877.5 (active)>", 
    "<NSLayoutConstraint:0x174092660 V:[_UILayoutGuide:0x10143e8d0]-(0)-[UIView:0x101439120] (active)>", 
    "<NSLayoutConstraint:0x1740927a0 V:[UIView:0x101439120]-(0)-[_UILayoutGuide:0x10143ddc0] (active)>", 
    "<NSLayoutConstraint:0x174093380 V:|-(0)-[UIImageView:0x101432d20] (active, names: '|':UIView:0x101439120)>", 
    "<NSLayoutConstraint:0x174093650 UIImageView:0x101432d20.bottom == UIView:0x101439120.bottom (active)>", 
    "<NSLayoutConstraint:0x170098830 'UIView-Encapsulated-Layout-Height' UIView:0x10142f4a0.height == 667 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174093650 UIImageView:0x101432d20.bottom == UIView:0x101439120.bottom (active)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

私はデバッグビュー階層

以下のリンクされた画像を示してデバッグするのXcodeを使用する場合:親ラッパーのUIView上の

  1. 制約を。
  2. ストーリーボードを介して作成されたUIImageViewの制約。
  3. Swiftによって作成されたUIImageViewの制約。あなたのコンソールログに

constraint values

+1

この行を追加し 'swiftImageView.translatesAutoresizingMaskIntoConstraints = false'を – Maddy

+0

!ありがとうございました。 :-) –

答えて

2

、これらの2行は何が起こっているのかを教えてくれ:

(注意:あなたが理解していない NSAutoresizingMaskLayoutConstraintsを見ている場合は、を参照してください。 UIViewプロパティのドキュメント は、変換マスクの制約を翻訳します。

NSAutoresizingMaskLayoutConstraint:0x101432d20.midYの== 877:0x17009cc50 H = - & UIImageView - = & V。5(アクティブ)

あなたのコードに次の行を追加し、物事を修正します:

それをやった
swiftImageView.translatesAutoresizingMaskIntoConstraints = false 
関連する問題