【问题标题】:Swift - Container view not resizing on orientation changeSwift - 容器视图未根据方向更改调整大小
【发布时间】:2020-10-10 05:41:31
【问题描述】:

我看到了几个与此问题相关的问题,但没有适用于我的案例的答案。

我在容器 (sampleContainer) 内有一个视图控制器 (sampleVC),无论您从哪个位置开始,它都能完美加载,但在从横向更改为纵向或反之亦然后,它不会调整大小。

在我的视图控制器中,我有:

    func addSampleVC() {
        self.addChild(self.sampleVC)
        self.sampleVC.didMove(toParent: self)
        self.sampleContainer.addSubview(self.sampleVC.view)
        self.addSampleConstraints()
    }

   func addSampleConstraints() {
        sampleContainer.bounds = CGRect(x: 0, y: -20, width: sampleContainer.bounds.width, height: sampleContainer.bounds.height)
        sampleVC.view.bounds = CGRect(x: 0, y: 0, width: sampleContainer.bounds.width * 0.94, height: sampleContainer.bounds.height)
        sampleVC.view.leftAnchor.constraint(equalTo: sampleContainer.leftAnchor, constant: 20).isActive = true
        sampleContainer.clipsToBounds = true

        NSLayoutConstraint.constrain(view: sampleVC.view, containerView: sampleContainer)
    }

函数 addSampleVC 从 viewDidLoad 调用。根据其他答案,我尝试在 viewWillTransition 中获取更新的容器边界,然后调用 setNeedsLayout 和 setNeedsUpdate,但我一定遗漏了一些东西,因为似乎没有任何效果。感谢您的任何想法。

【问题讨论】:

    标签: ios swift ipad orientation


    【解决方案1】:

    我已经处理过一次视图容器,所以我会尽力帮助您。

    您可以选择使用自动布局或“手动”布局(又称自动调整大小约束)。在您的代码中,在 addSampleConstraints 中,第 3 行中的左锚语句将被忽略,因为未明确设置自动布局。

    1. 手动布局:
        func addSampleVC() {
            self.addChild(self.sampleVC)
            self.sampleContainer.addSubview(self.sampleVC.view)
            self.addSampleConstraints()
            self.sampleVC.didMove(toParent: self)
        }
    
       func addSampleConstraints() {
            sampleContainer.bounds = CGRect(x: 0, y: -20, width: sampleContainer.bounds.width, height: sampleContainer.bounds.height)
            sampleVC.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
            sampleVC.view.frame = CGRect(x: 0, y: 0, width: sampleContainer.bounds.width * 0.94, height: sampleContainer.bounds.height)
            sampleContainer.clipsToBounds = true
        }
    
    1. 自动布局(第一个功能同1.):
       func addSampleConstraints() {
            sampleContainer.translatesAutoResizingMaskIntoConstraints = false // Setting Auto layout
            sampleVC.view.translatesAutoResizingMaskIntoConstraints = false // Setting Auto layout
            sampleVC.view.topAnchor.constraint(equalTo: sampleContainer.topAnchor).isActive = true
            sampleVC.view.bottomAnchor.constraint(equalTo: sampleContainer.bottomAnchor).isActive = true
            sampleVC.view.widthAnchor.constraint(equalTo: sampleContainer.widthAnchor, multiplier: 0.94).isActive = true
            sampleVC.view.leftAnchor.constraint(equalTo: sampleContainer.leftAnchor, constant: 20).isActive = true
            sampleContainer.clipsToBounds = true
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 2023-03-16
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多