ORStudios
ORStudios

Reputation: 3233

Disabling Auto Layout For A Single UIImageView

I have a UIImageView in my photo app that changes size based upon the imported image. The size is created dynamically and then positioned programmatically in the center of the screen without using auto layout.

Now to do this I have used

self.imageViewCanvas.translatesAutoresizingMaskIntoConstraints = YES;
[self.imageViewCanvas setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];

to disable the constraints. The problem is that whenever I load the app I get a long warning message that contains the following:

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "" ) Will attempt to recover by breaking constraint Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

Is there a way to disable this particular warning or can I adjust the code in some way to avoid it?

Thanks

Upvotes: 0

Views: 831

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90551

For a storyboard or a NIB with auto layout enabled, Xcode will provide constraints that are missing. This happens at build time. If you look at the Size inspector for a view to which you haven't added any constraints, you'll see a message about Xcode adding constraints for you. So, basically, no matter what you do, the image view will be constrained.

If you then set translatesAutoresizingMaskIntoConstraints to true, that will conflict with the automatically-supplied constraints. You could remove/deactivate those constraints, but it's hard to obtain references to them.

Your options:

  • Use auto layout to position the image view. It's easy to accomplish what you want.
  • Turn off auto layout for the whole NIB, so there will be no constraints and translatesAutoresizingMaskIntoConstraints will be true by default.
  • Add sufficient constraints of your own in the NIB but mark them to be removed at build time on the Attributes or Size inspector. This signals to Xcode that you want to take over and prevents it from supplying its own constraints. (This is normally done when you will be supplying constraints programmatically, but it can also work if you're just going to turn on translatesAutoresizingMaskIntoConstraints.)
  • Add constraints of your own and set up outlets to them. Deactivate them programmatically before turning on translatesAutoresizingMaskIntoConstraints.

Upvotes: 1

Related Questions