凡更新自动布局约束大小的变化是什么时候? [英] Where to update Autolayout constraints when size changes?

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

问题描述

我有几个的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.

我不知道那里的code键更改约束不断需要去。它需要设置旋转设备时在屏幕上出现的第一次和更新的键盘前

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.

我的问题是:哪里是最合适的地方把这个code?有没有更好的方法来计算宽度将是什么?我可以保证它永远是完全相同的宽度屏幕宽度。而我使用的大小类在X code 6,因此 willRotateToInterfaceOrientation 和类似的方法去precated。

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. 相等宽度约束添加到所有的人

  4. 添加约束0宽度双方之间的间距和对方

  5. 降低一个或一个以上的宽度相等约束以防万一宽度不能等分的优先级。

然后自动布局将处理一切为你。

Then auto layout will handle everything for you.

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

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