<tfoot id='9LL8Y'></tfoot>
    <legend id='9LL8Y'><style id='9LL8Y'><dir id='9LL8Y'><q id='9LL8Y'></q></dir></style></legend>
    • <bdo id='9LL8Y'></bdo><ul id='9LL8Y'></ul>

      <small id='9LL8Y'></small><noframes id='9LL8Y'>

    1. <i id='9LL8Y'><tr id='9LL8Y'><dt id='9LL8Y'><q id='9LL8Y'><span id='9LL8Y'><b id='9LL8Y'><form id='9LL8Y'><ins id='9LL8Y'></ins><ul id='9LL8Y'></ul><sub id='9LL8Y'></sub></form><legend id='9LL8Y'></legend><bdo id='9LL8Y'><pre id='9LL8Y'><center id='9LL8Y'></center></pre></bdo></b><th id='9LL8Y'></th></span></q></dt></tr></i><div id='9LL8Y'><tfoot id='9LL8Y'></tfoot><dl id='9LL8Y'><fieldset id='9LL8Y'></fieldset></dl></div>

      UIImageView .scaleAspectFit 和自动布局无法从 Swift 以编程方式工作

      时间:2023-09-09

          <bdo id='QpZoN'></bdo><ul id='QpZoN'></ul>
            <tbody id='QpZoN'></tbody>
          <tfoot id='QpZoN'></tfoot>

              <legend id='QpZoN'><style id='QpZoN'><dir id='QpZoN'><q id='QpZoN'></q></dir></style></legend>
              <i id='QpZoN'><tr id='QpZoN'><dt id='QpZoN'><q id='QpZoN'><span id='QpZoN'><b id='QpZoN'><form id='QpZoN'><ins id='QpZoN'></ins><ul id='QpZoN'></ul><sub id='QpZoN'></sub></form><legend id='QpZoN'></legend><bdo id='QpZoN'><pre id='QpZoN'><center id='QpZoN'></center></pre></bdo></b><th id='QpZoN'></th></span></q></dt></tr></i><div id='QpZoN'><tfoot id='QpZoN'></tfoot><dl id='QpZoN'><fieldset id='QpZoN'></fieldset></dl></div>
              1. <small id='QpZoN'></small><noframes id='QpZoN'>

              2. 本文介绍了UIImageView .scaleAspectFit 和自动布局无法从 Swift 以编程方式工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有几个部分透明的 PNG 叠加层可以在视图中相互叠加显示.覆盖的数量取决于运行时条件.我想在 Swift 中创建 UIImageView 实例并将它们添加到视图中.

                I have several partly transparent PNG overlays to display overlaying one another in a view. The number of overlays varies depending on runtime conditions. I want to create the UIImageView instances in Swift and add them to the view.

                在情节提要中添加 UIImageView 并通过 Swift 设置其图像时,我可以轻松获得我想要的行为.在这种情况下,我将 UIImageView 的前导、尾随、顶部和底部固定到父包装器 UIView 的前导、尾随、顶部和底部.在 UIImageView 的属性检查器中,我将内容模式"设置为Aspect Fit",当 Swift 设置图像时,大图像会很好地缩小.用户可以在 iPhone 的小屏幕上看到整个图像.

                I don't have any trouble getting the behaviour I want when adding an UIImageView in the storyboard and setting its image via Swift. In such a case, I pin the leading, trailing, top, and bottom of the UIImageView to the leading, trailing, top, and bottom of the parent wrapper UIView. In the attribute inspector for the UIImageView, I set "Content Mode" to "Aspect Fit" and the large image is scaled down nicely when Swift sets the image. The user can see the whole image on the little iPhone screen.

                当我动态创建 UIImageView 并将其添加到同一个父包装器 UIView 时,我遇到了约束问题.有太多的约束需要满足.

                When I dynamically create a UIImageView and add it to the same parent wrapper UIView, I'm having a problem with constraints. There are too many constraints to satisfy.

                我不明白为什么通过 Swift 创建的 UIImageView 与通过情节提要创建的 UIImageView 不同.

                I cannot figure out why the UIImageView created via Swift is different from the UIImageView created via the storyboard.

                如何让通过 Swift 创建的 UIImageView 的行为与通过情节提要创建的 UIImageView 一样?

                How can I get the UIImageView created via Swift to behave just like the UIImageView created via the storyboard?

                在通过情节提要创建的 UIImageView 和通过 Swift 创建的 UIImageView 上设置图像的 Swift 代码.

                The Swift code that sets the image on the UIImageView created via the storyboard and the UIImageView created via Swift.

                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 中看到的输出.

                The output I see in Xcode when the layout manager is trying to satisfy conflicting constraints.

                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 调试 Debug View Hierarchy 时

                When I use Xcode to debug the Debug View Hierarchy

                以下链接图片显示:

                1. 父包装 UIView 的约束.
                2. 通过情节提要创建的 UIImageView 上的约束.
                3. 通过 Swift 创建的 UIImageView 的约束.

                约束值

                推荐答案

                在你的控制台日志中,这两行告诉你发生了什么:

                In your console log, these two lines tell you what's happening:

                (注意:如果您看到 NSAutoresizingMaskLayoutConstraints不明白,参考UIView属性的文档translatesAutoresizingMaskIntoConstraints)

                (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

                .

                NSAutoresizingMaskLayoutConstraint:0x17009cc50 h=--&v=--&UIImageView:0x101432d20.midY == 877.5(活动)

                NSAutoresizingMaskLayoutConstraint:0x17009cc50 h=--& v=--& UIImageView:0x101432d20.midY == 877.5 (active)

                将此行添加到您的代码中将解决问题:

                Adding this line to your code will fix things:

                swiftImageView.translatesAutoresizingMaskIntoConstraints = false
                

                这篇关于UIImageView .scaleAspectFit 和自动布局无法从 Swift 以编程方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在设备旋转时保持自动布局约束处于活动状态 下一篇:iOS - 将两个标签放在其超级视图的中心

                相关文章

                <legend id='uXvdM'><style id='uXvdM'><dir id='uXvdM'><q id='uXvdM'></q></dir></style></legend>

                <small id='uXvdM'></small><noframes id='uXvdM'>

                  <bdo id='uXvdM'></bdo><ul id='uXvdM'></ul>

                <tfoot id='uXvdM'></tfoot>

                  1. <i id='uXvdM'><tr id='uXvdM'><dt id='uXvdM'><q id='uXvdM'><span id='uXvdM'><b id='uXvdM'><form id='uXvdM'><ins id='uXvdM'></ins><ul id='uXvdM'></ul><sub id='uXvdM'></sub></form><legend id='uXvdM'></legend><bdo id='uXvdM'><pre id='uXvdM'><center id='uXvdM'></center></pre></bdo></b><th id='uXvdM'></th></span></q></dt></tr></i><div id='uXvdM'><tfoot id='uXvdM'></tfoot><dl id='uXvdM'><fieldset id='uXvdM'></fieldset></dl></div>