菜鸟教程小白 发表于 2022-12-11 18:10:18

ios - 如何在方向更改时更新 UIView 子类中的自动布局约束


                                            <p><p>我创建了一个 <code>UIView</code> 子类,并在其中添加了一些 imageView 和标签。我使用 Visual Format 来创建 <code>constraints</code>。并且 View 布局对于不同的大小类是不同的。我使用下面的代码来识别尺寸等级。</p>

<pre><code>let rule = UITraitCollection(horizontalSizeClass: .regular)
let isHorizontalRegular = self.traitCollection.containsTraits(in: rule)
if isHorizontalRegular {
// constraints forHorizontal Regular size class
} else {
// constraints forother size class
}
</code></pre>

<p>由于我没有在 <code>init</code> 方法中获取尺寸类信息,所以我创建了所有自动布局 <code>constraints</code> 都是在 <code>updateConstraints()</code> 中创建的方法。</p>

<p>基于这个实现,我有一些问题。</p>

<ol>
<li>在重写方法 <code>updateConstraints()</code> 中创建所有 <code>constraint</code> 是否正确?</li>
<li>如果不是,那是西方式根据size class创建自动布局<code>constraints</code>。</li>
<li>如何在方向更改时更新自动布局<code>constraints</code>。(因为我使用视觉格式来创建自动布局<code>constraints</code>)?</li>
</ol>

<p>我知道我可以使用 <code>UIViewController</code> 生命周期方法解决我的问题。但我使用 <code>@IBDesignable</code> 在 Storyboard 上呈现 View 。那么是否有可能在 <code>UIView</code> 子类中实现这些。</p>

<p>请帮我解决这些问题。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>小心,一些链接继续 Obj-C 即将出现。</p>

<p>第一步是学习整个 View 生命周期,<a href="https://rdkw.wordpress.com/2013/02/24/ios-uiviewcontroller-lifecycle/" rel="noreferrer noopener nofollow">this should help</a> .下一步是了解 <a href="http://imnotyourson.com/ios-adaptive-layout-with-rotation-tips/" rel="noreferrer noopener nofollow">adaptive layout</a> 是什么是。</p>

<p>关注这些覆盖:</p>

<p><strong>viewDidLoad():</strong>
这是您可以设置约束的地方。确保 subview 已加载并且 translatesAutoresizingMaskIntoConstraints 对所有内容都是错误的。</p>

<p><strong>viewWillLayoutSubviews():</strong>
我倾向于在这里更改方向,但这只是因为我需要知道整体边界是纵向(高度>宽度)还是横向(宽度>高度)。听起来你有一个更好的地方......</p>

<p><strong>viewWillTransition(to size: with coordinator:):</strong>
这是 <strong>任何</strong> 尺寸类更改将发生或不发生的地方。 iPad 也可以滑出或 Split View。请注意,基本的 iPad 方向更改<strong>不会</strong>意味着尺寸等级的更改,因为它们是常规的/常规的。尽管如此,此功能仍将被 ping 通。 </p>

<p>这是我通常做的模板:</p>

<pre><code>var p = ()
var l = ()
var initialOrientation = true
var isInPortrait = false

override func viewDidLoad() {
    super.viewDidLoad()
    setUpConstraints()
}   

override func viewWillLayoutSubviews() {
    super.viewDidLayoutSubviews()
    if initialOrientation {
      initialOrientation = false
      if view.frame.width &gt; view.frame.height {
            isInPortrait = false
      } else {
            isInPortrait = true
      }
      orientationChanged()
    } else {
      if view.orientationHasChanged(&amp;isInPortrait) {
            orientationChanged()
      }
    }
}
func setUpConstraints() {
    view.turnOffAutoResizing()
    // append your portrait (p) and landscape (l) constraint arrays here
}

extension UIView {

    public func turnOffAutoResizing() {
      self.translatesAutoresizingMaskIntoConstraints = false
      for view in self.subviews as {
            view.translatesAutoresizingMaskIntoConstraints = false
      }
    }
    public func orientationHasChanged(_ isInPortrait:inout Bool) -&gt; Bool {
      if self.frame.width &gt; self.frame.height {
            if isInPortrait {
                isInPortrait = false
                return true
            }
      } else {
            if !isInPortrait {
                isInPortrait = true
                return true
            }
      }
      return false
    }
    public func setOrientation(_ p:, _ l:) {
      NSLayoutConstraint.deactivate(l)
      NSLayoutConstraint.deactivate(p)
      if self.bounds.width &gt; self.bounds.height {
            NSLayoutConstraint.activate(l)
      } else {
            NSLayoutConstraint.activate(p)
      }
    }
}
</code></pre>

<p>这对于您的需求可能有点矫枉过正,但您应该能够了解它的要点。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在方向更改时更新 UIView 子类中的自动布局约束,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40825824/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40825824/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在方向更改时更新 UIView 子类中的自动布局约束