大小更改时在哪里更新自动布局约束? [英] Where to update Autolayout constraints when size changes?

查看:23
本文介绍了大小更改时在哪里更新自动布局约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在包含 UIView 的底部放置了几个 UIView.我希望这些视图的宽度始终相等,并且始终拉伸以共同填充包含视图的宽度(例如底部的表情符号键盘按钮).我解决这个问题的方法是为其中一个视图设置相等的宽度,然后将该视图的宽度约束更新为 superviewWidth/numberOfViews 这将导致所有其他视图更新为相同的值.

I have several UIViews laid out along the bottom of a containing UIView. I want these views to always be equal width, and always stretch to collectively fill the width of the containing view (like the emoji keyboard buttons at the bottom). The way I'm approaching this is to set equal widths to one of the views, then just update the width constraint of that view to be superviewWidth / numberOfViews which will cause all of the other views to update to that same value.

我想知道更改约束常量的代码需要去哪里.需要在键盘第一次出现在屏幕上之前设置,并在旋转设备时更新.

I am wondering where the code to change the constraint constant needs to go. It needs to be set before the keyboard appears on screen for the first time and update when rotating the device.

我对解决方案的第一次尝试是将其放入 updateViewConstraints 并通过 containerView.frame.size.width 计算宽度.但是这个方法在加载时被调用了两次,第一次它正确地计算了值,但是第二次由于某种原因 containerView 的宽度是 0.0.另一个问题是,当旋转时,containerView 的宽度不是旋转后的值,而是旋转前的当前值.但我不想等到旋转完成后才更新约束,因为按钮将是原始大小,然后会改变这对用户来说会很刺耳.

My first attempt at a solution was to place it in updateViewConstraints and calculate the width via containerView.frame.size.width. But this method is called twice upon load, the first time it calculates the values correctly, but the second time for some reason the containerView's width is 0.0. Another issue is that when rotating, the containerView's width is not the value that it will be after rotation, it's the current value before rotation. But I don't want to wait until after the rotation completes to update the constraint, because the buttons will be the original size then change which will be jarring to the user.

我的问题是:在哪里放置这段代码最合适?有没有更好的方法来计算宽度是多少?我可以保证它总是与屏幕宽度完全相同.而且我在 Xcode 6 中使用 Size Classes,因此不推荐使用 willRotateToInterfaceOrientation 和类似的方法.

My question is: where is the most appropriate place to put this code? Is there a better way to calculate what the width will be? I can guarantee it will always be the exact same width as the screen width. And I am using Size Classes in Xcode 6, so willRotateToInterfaceOrientation and similar methods are deprecated.

推荐答案

没有理由手动更新宽度:

There is no reason to update the width manually:

  1. 将所有宽度相等的视图放在视图中,彼此之间没有间距
  2. 为它们添加等宽约束
  3. 为边和彼此之间的间距添加宽度为 0 的约束
  4. 降低一个或多个等宽约束的优先级,以防无法等宽.

然后自动布局将为您处理一切.

Then auto layout will handle everything for you.

这篇关于大小更改时在哪里更新自动布局约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆