38

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).

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

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

Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
Jon Cox
  • 10,622
  • 22
  • 78
  • 123

4 Answers4

22

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

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.

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

enter image description here

Community
  • 1
  • 1
CocoaBob
  • 533
  • 4
  • 9
  • This is a slick solution, but I believe it was causing a "freeze" in XCode 6.3 beta 2. When I removed it, my view loaded properly.. but with it, it was crashing after I upgraded to beta 2. – Steve N Mar 07 '15 at 22:59
  • 2
    This works correctly in XCode 9 and iOS 11 for anyone that's looking for an updated solution. This should be the accepted answer now. – JasonD Oct 01 '17 at 18:35
17

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:

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.

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.

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

redlightbulb
  • 1,466
  • 11
  • 9
  • How do you use a category to set all UIView object to return no? Category method only applies to an instance. – Boon Oct 04 '14 at 15:19
  • 4
    This answer has been accepted, but did it work? I would expect this approach to be problematic. – Douglas Hill Nov 13 '14 at 12:58
  • The category method clobbers the original method, @Boon, and each instance uses the new implementation. [Unless the original method was itself implemented in a category](http://stackoverflow.com/a/7013922/603977). – jscs Aug 27 '15 at 04:40
5

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

[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

Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • Nice, but I'm guessing you could end up with conflicts doing it this way, instead of thinking about working with the existing constraints. – Danyal Aytekin Jan 03 '14 at 10:50
5

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

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

kwl
  • 495
  • 6
  • 13