在Xcode 4.5中,如何设置translatesautoresizingmaskintoconstraint

时间:2021-02-14 22:57:55

I need to set translatesAutoresizingMaskIntoConstraints to NO. By default it is set to YES (to assist with the majority of apps that are transitioning from struts and springs to the new Auto Layout).

我需要将translatesautoresizingmaskintoconstraint设置为NO。默认情况下,它设置为YES(以帮助大多数应用程序从struts过渡到新的自动布局)。

Is there somewhere in Xcode where the default can be changed from YES to NO?

Xcode中是否存在可以将默认值从YES更改为NO的地方?

Or do I have to manually set it for every view?

还是我必须为每个视图手动设置它?

4 个解决方案

#1


18  

This is a great question - and one I've tried to find an answer to myself. Sadly, it looks like there is no "quick fix". Currently, Apple considers Constraint-based layout Opt-in - even naming a section of the UIView Class Reference:

这是一个很好的问题——我一直在努力寻找自己的答案。不幸的是,看起来没有“快速修复”。目前,苹果考虑了基于约束的布局选择——甚至命名了UIView类引用的一个部分:

Opting in to Constraint-Based Layout

选择基于约束的布局

But that Opt-in is not global. I presume this is because not everything looks good if you just turn Springs & Struts into Constraints. Some UI elements break, or you would get a ton of unsatisfiable constraints errors.

但这种选择并不是全球性的。我认为这是因为如果把spring和Struts转换成约束条件,并不是所有的东西看起来都很好。一些UI元素会中断,或者您会得到大量无法满足的约束错误。

I can think of one possible solution - I have not tried it myself, but you could make a category on UIView that sets all UIView objects to return NO for - (BOOL)translatesAutoresizingMaskIntoConstraints. While I do not know what this would break, it would globally set translatesAutoresizingMaskIntoConstraints to NO.

我可以想出一个可能的解决方案——我自己还没有尝试过,但是你可以在UIView上创建一个类别,它设置所有的UIView对象返回NO (BOOL) translatesautoresizingmaskintoconstraint。虽然我不知道它会中断什么,但它会在全局上设置翻译自适应maskintoconstraints(不)。

Here is a good introduction to Categories if you want to learn more about them!

如果你想了解更多关于类别的信息,这里有一个很好的分类介绍。

#2


19  

I'm late to this question, but the mentioned option is still missing in Xcode 5 & 6, so the question is still meaningful.

我回答这个问题有点晚了,但是Xcode 5和6中仍然没有提到这个选项,所以这个问题仍然是有意义的。

Actually, we can always set a value to any property of a view/control/object by adding a User Defined Runtime Atribute in Storyboard (Interface Builder) like the following screenshot.

实际上,我们可以通过在故事板(接口构建器)中添加用户定义的运行时Atribute(接口构建器)来设置视图/控件/对象的任何属性的值,如下面的截图所示。

And it also works for translatesAutoresizingMaskIntoConstraints. So the question could be solved.

它也适用于翻译自适应maskintoconstraint。这样问题就可以解决了。

在Xcode 4.5中,如何设置translatesautoresizingmaskintoconstraint

#3


4  

When u have to change the size or position of your subview. Use (BOOL)translatesAutoresizingMaskIntoConstraints method before you set the frame of your subview.

当你需要改变子视图的大小或位置时。在设置子视图的框架之前,使用(BOOL)translatesAutoresizingMaskIntoConstraints方法。

[self.benchmarkButton removeFromSuperview];
[self.benchmarkButton setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.benchmarkButton setFrame:CGRectMake(20, self.benchmarkButton.frame.origin.y+40, 260, 30)];
[self.benchmarksView addSubview:self.benchmarkButton];

Thats way your subview will not fight from constraints as it is default (AutoLayout) in Xcode 4.3 and later. Thanks

这样,您的子视图就不会与约束对抗,因为它是Xcode 4.3和以后默认的(AutoLayout)。谢谢

#4


3  

According to the documentation, this property is automatically set to NO if the view is added through Interface Builder.

根据文档,如果通过Interface Builder添加视图,则此属性将自动设置为NO。

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/translatesAutoresizingMaskIntoConstraints

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/ / / apple_ref / occ instp / UIView / translatesAutoresizingMaskIntoConstraints

#1


18  

This is a great question - and one I've tried to find an answer to myself. Sadly, it looks like there is no "quick fix". Currently, Apple considers Constraint-based layout Opt-in - even naming a section of the UIView Class Reference:

这是一个很好的问题——我一直在努力寻找自己的答案。不幸的是,看起来没有“快速修复”。目前,苹果考虑了基于约束的布局选择——甚至命名了UIView类引用的一个部分:

Opting in to Constraint-Based Layout

选择基于约束的布局

But that Opt-in is not global. I presume this is because not everything looks good if you just turn Springs & Struts into Constraints. Some UI elements break, or you would get a ton of unsatisfiable constraints errors.

但这种选择并不是全球性的。我认为这是因为如果把spring和Struts转换成约束条件,并不是所有的东西看起来都很好。一些UI元素会中断,或者您会得到大量无法满足的约束错误。

I can think of one possible solution - I have not tried it myself, but you could make a category on UIView that sets all UIView objects to return NO for - (BOOL)translatesAutoresizingMaskIntoConstraints. While I do not know what this would break, it would globally set translatesAutoresizingMaskIntoConstraints to NO.

我可以想出一个可能的解决方案——我自己还没有尝试过,但是你可以在UIView上创建一个类别,它设置所有的UIView对象返回NO (BOOL) translatesautoresizingmaskintoconstraint。虽然我不知道它会中断什么,但它会在全局上设置翻译自适应maskintoconstraints(不)。

Here is a good introduction to Categories if you want to learn more about them!

如果你想了解更多关于类别的信息,这里有一个很好的分类介绍。

#2


19  

I'm late to this question, but the mentioned option is still missing in Xcode 5 & 6, so the question is still meaningful.

我回答这个问题有点晚了,但是Xcode 5和6中仍然没有提到这个选项,所以这个问题仍然是有意义的。

Actually, we can always set a value to any property of a view/control/object by adding a User Defined Runtime Atribute in Storyboard (Interface Builder) like the following screenshot.

实际上,我们可以通过在故事板(接口构建器)中添加用户定义的运行时Atribute(接口构建器)来设置视图/控件/对象的任何属性的值,如下面的截图所示。

And it also works for translatesAutoresizingMaskIntoConstraints. So the question could be solved.

它也适用于翻译自适应maskintoconstraint。这样问题就可以解决了。

在Xcode 4.5中,如何设置translatesautoresizingmaskintoconstraint

#3


4  

When u have to change the size or position of your subview. Use (BOOL)translatesAutoresizingMaskIntoConstraints method before you set the frame of your subview.

当你需要改变子视图的大小或位置时。在设置子视图的框架之前,使用(BOOL)translatesAutoresizingMaskIntoConstraints方法。

[self.benchmarkButton removeFromSuperview];
[self.benchmarkButton setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.benchmarkButton setFrame:CGRectMake(20, self.benchmarkButton.frame.origin.y+40, 260, 30)];
[self.benchmarksView addSubview:self.benchmarkButton];

Thats way your subview will not fight from constraints as it is default (AutoLayout) in Xcode 4.3 and later. Thanks

这样,您的子视图就不会与约束对抗,因为它是Xcode 4.3和以后默认的(AutoLayout)。谢谢

#4


3  

According to the documentation, this property is automatically set to NO if the view is added through Interface Builder.

根据文档,如果通过Interface Builder添加视图,则此属性将自动设置为NO。

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instp/UIView/translatesAutoresizingMaskIntoConstraints

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/ / / apple_ref / occ instp / UIView / translatesAutoresizingMaskIntoConstraints