在code修改故事板自动布局约束的最佳实践? [英] Best practice for modifying storyboard auto layout constraints in code?

查看:105
本文介绍了在code修改故事板自动布局约束的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做我的UI在code,但已决定,我应该用故事板和经销商的布局目前的一个项目。一切都进展顺利,直到我建立了一个复杂的场景有很多层次和一些意见电网约50看法。

I've always done my UIs in code but have decided that I should use storyboards and auto layout for a current project. Everything had been going well until I built a complex scene with about 50 views with lots of hierarchy and some grid of views.

问题是,我的自动布局得到了一些设备和取向混乱。我发现它具有挑战性的使用IB尝试约束固定的几十(几百?),或者追查问题并加以解决。情况是这样的,我没有收到错误或警告,只是有时候一些不愉快的布局。和IB可以与你需要做的跟踪约束信息,更不用说获得他们都在一个场景是如何与一个完整的想法的所有点击和更改设置的痛苦。

The problem is that my auto layout is getting muddled on some devices and orientations. I'm finding it challenging to use IB to try fixing the dozens (hundreds?) of constraints or to track down the problems and resolve them. The situation is such that I'm not getting errors or warnings, just some unpleasant layouts at times. And IB can be a pain with all the clicking and changing settings you need to do to track down constraint information, let alone get a full idea of how they all relate in a scene.

我刚刚花了一天时间阅读自动布局和约束文档和背景材料,它似乎我最好的解决办法是使用可视化格式来指定在code约束和创建一些自定义的code,以帮助。不过,我似乎无法找到如何从IB至code的转移东西。

I've just spent a day reading docs and background material on auto layout and constraints and it seems my best solution is to use the visual format to specify constraints in code and create some custom code to help. However, I can't seem to find anything on how to make the transition from IB to code.

具体而言,应该我擦所有的IB限制,并用手做他们的全部或是否有可能是有选择性的?我问,因为我有意见的一些团体含意见,并在内容的看法有一个完美的布局。

Specifically, should I wipe all IB constraints and do them all by hand or is it possible to be selective? I ask because I have some groups of views in containing views where the content views have a perfect layout.

其次,最好在哪里我把我的code?我想共存故事板,只想选择性地修改一些复杂的场景。是一个视图控制器的viewWillAppear中:在正确的地方修改或删除/添加约束为它所控制的看法

Secondly, where best do I put my code? I want to coexist storyboards and just want to selectively modify some complex scenes. Is a view controller's viewWillAppear: the right place to modify or remove/add constraints for the view it controls?

推荐答案

不好意思拿这么这么久才回本,而其他项目的侵入。

Sorry to take so so long to get back to this while other projects intruded.

我不得不做大量的重构来简化我的场景,这样的自动布局可以做正确的事情,但我并不完全满意的结果。这个问题似乎是,IB是不容易的,有很多的项目使用,而自动布局复杂,必然。

I had to do a lot of refactoring to simplify my scenes so that auto layout could do the right thing, and yet I am not fully satisfied with the results. The problem seems to be that IB is just not easy to use with lots of items, and that auto layout is complicated, by necessity.

随着中说,到目前为止,我见过的最好的结果是从这篇文章由Justin德里斯科尔得出:<一href=\"http://themainthread.com/blog/2014/02/building-a-universal-app.html\">http://themainthread.com/blog/2014/02/building-a-universal-app.html

With that said, the best results I've seen so far are drawn from this article by Justin Driscoll: http://themainthread.com/blog/2014/02/building-a-universal-app.html

他主张建立自定义视图来封装可重用的UI组件。我采取这种方法,但延长了主意,还捆绑了这不会很不同的布局,布局变化相关的零部件。例如,我有一个按钮和两个标签一个进度条,所以尽管我不重用他们作为一个群体,他们需要的是相邻和概念是相关的,所以我做了他们的自定义视图用于处理汽车布局贾斯汀建议。

He advocates building custom views to encapsulate reusable UI components. I have taken this approach but have extended the idea to also bundle up related components that are not going to layout very differently as the layout changes. For example, I have a progress bar with button and two labels, so even though I am not reusing them as a group, they need to be adjacent and conceptually are related, so I've made a custom view for them which handles the auto layout as Justin suggests.

我现在采取了自动布局的每个级别只能有元素屈指可数的办法。如果一个人水平得到太复杂,我将在自定义视图中捆绑了一些相关项目,推动新的视图内一些汽车布局。到目前为止,这是不是太糟糕了。

I'm now taking the approach that each level of auto layout should only have a handful of elements. If one level gets too complex, I'll bundle up some related items in a custom view and push some auto layout inside that new view. So far it isn't too bad.

这篇关于在code修改故事板自动布局约束的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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