ios 在 Xcode 4.5 中设置 translatesAutoresizingMaskIntoConstraints 的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14204082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:45:31  来源:igfitidea点击:

Where to set translatesAutoresizingMaskIntoConstraints in Xcode 4.5

iosxcodeios6xcode4.5autolayout

提问by Jon Cox

I need to set translatesAutoresizingMaskIntoConstraintsto 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).

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

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

Xcode 中是否有某个地方可以将默认值从 更改YESNO

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

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

采纳答案by redlightbulb

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 UIViewClass Reference:

这是一个很好的问题 - 我试图为自己找到答案。可悲的是,看起来没有“快速修复”。目前,Apple 考虑基于约束的布局选择加入 - 甚至命名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.

但该选择加入不是全球性的。我认为这是因为如果您只是将 Springs & Struts 转换为 Constraints,则并非一切都看起来不错。一些 UI 元素会损坏,或者您会收到大量无法满足的约束错误。

I can think of one possiblesolution - I have not tried it myself, but you could make a category on UIViewthat sets allUIViewobjects to return NOfor - (BOOL)translatesAutoresizingMaskIntoConstraints. While I do not know what this would break, it would globally set translatesAutoresizingMaskIntoConstraintsto NO.

我可以想到一种可能的解决方案 - 我自己没有尝试过,但是您可以创建一个类别UIView,将所有UIView对象设置NO- (BOOL)translatesAutoresizingMaskIntoConstraints. 虽然我不知道这会破坏什么,但它会全局设置translatesAutoresizingMaskIntoConstraintsNO.

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

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

回答by CocoaBob

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 Atributein Storyboard (Interface Builder) like the following screenshot.

实际上,我们始终可以通过在 Storyboard(界面生成器)中添加用户定义的运行时属性来为视图/控件/对象的任何属性设置值,如下面的屏幕截图所示。

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

它也适用于translatesAutoresizingMaskIntoConstraints。那么问题就可以解决了。

enter image description here

在此处输入图片说明

回答by Muhammad Aamir Ali

When u have to change the size or position of your subview. Use (BOOL)translatesAutoresizingMaskIntoConstraintsmethod 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 及更高版本中的默认值(自动布局)。谢谢

回答by kwl

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

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

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