如何在自动布局和创建框架之间取得平衡? [英] How do you balance Auto Layout with creating frames?

查看:83
本文介绍了如何在自动布局和创建框架之间取得平衡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用CGRectMake()在iOS中创建框架时,无论是设置frame属性还是使用initWithFrame:,它都会要求提供x,y,宽度和高度.我提供这些.

When I'm creating a frame in iOS with CGRectMake(), be it setting the frame property or using initWithFrame:, it asks for the x, y, width and height. I supply these.

但是对于自动布局,我接下来要说的是:将其x放置在中间",然后将宽度设置为屏幕的一半",紧接着我用CGRectMake()设置了可能不同.

But with Auto Layout, I'm next going to say something like: "position its x in the very middle" and "set the width to half the screen", right after I set those with CGRectMake() likely differently.

如何将它们设置为CGRectMake()? 0?任意值?接近我想要的东西吗?

What do I set them as in CGRectMake()? 0? An arbitrary value? Something close to what I want?

推荐答案

使用自动布局时,您根本不应设置frame属性.您可以设置frame,但这样做没有任何目的,只会使您的代码产生误导,因为frame将被其根据约束条件计算的尺寸所取代.我建议使用约束完全定义控件的尺寸,并避免完全设置frame以避免混淆.

When using auto-layout, you shouldn't set the frame property at all. You can set the frame, but there's no purpose in doing so, and only makes your code misleading as the frame will be superseded by the dimensions it calculates from the constraints. I would suggest completely defining the dimensions of the controls using constraints and avoid setting the frame entirely to avoid confusion.

因此,如果要以编程方式创建视图,而不是initWithFrame,则只需执行init.另外,不要忘记将translatesAutoresizingMaskIntoConstraints设置为NO,并添加所有适当的约束,以使布局无歧义,但也没有冲突的约束.

So, if you're creating views programmatically, instead of initWithFrame, just do init. Also, do not forget to set translatesAutoresizingMaskIntoConstraints to NO, and add all of the appropriate constraints such that the layout is unambiguous, but with no conflicting constraints, either.

这篇关于如何在自动布局和创建框架之间取得平衡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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