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
Where to set translatesAutoresizingMaskIntoConstraints in Xcode 4.5
提问by Jon Cox
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).
我需要设置translatesAutoresizingMaskIntoConstraints
为NO
. 默认情况下,它设置为YES
(以协助大多数从 struts 和 springs 过渡到新自动布局的应用程序)。
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?
还是我必须为每个视图手动设置它?
采纳答案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 UIView
Class 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 UIView
that sets allUIView
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)translatesAutoresizingMaskIntoConstraints
. 虽然我不知道这会破坏什么,但它会全局设置translatesAutoresizingMaskIntoConstraints
为NO
.
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。那么问题就可以解决了。
回答by Muhammad Aamir Ali
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 及更高版本中的默认值(自动布局)。谢谢
回答by kwl
According to the documentation, this property is automatically set to NO
if the view is added through Interface Builder.
根据文档,NO
如果通过 Interface Builder 添加视图,则此属性会自动设置为。