3

I want to practice creating simple apps using no storyboard. I am able to do the constraints programmatically (slowly) but I want to also practice separating my code into MVC. Is there a particular place/method that I am supposed to write the programatic constraints? Or does it not matter?

RubberDucky4444
  • 2,330
  • 5
  • 38
  • 70
  • 1
    For instance you can put the constraints in a subclass of UIView (or UITableViewCell) and then add that view to your UIViewController or use the customer cell in your tableView. – Mat Apr 25 '17 at 21:53
  • @mat but there is no "best practice" or built in method that constraints are supposed to be put into? – RubberDucky4444 Apr 25 '17 at 21:55
  • 1
    I personally create a method called `setupViews()` where I add all the subviews and constraints and then I call that method in the init function. I am not sure this is the best practice though ;) – Mat Apr 25 '17 at 21:58
  • 1
    MVC was a good starting point, then I switched to VIPER, now I practice object oriented developing. There are no models, wireframes or something like that anymore. Now I only code objects (did I say only?). See my comment there: http://stackoverflow.com/a/43426337/6595536 Maybe it's an idea for you how it could be. Note: The constraints are defined in my ViewRepresentation objects. – ObjectAlchemist Apr 25 '17 at 22:07
  • Proper place for setup constrains is UIViewController.updateViewConstrains or in UIViewController.viewDidLoad, if constraints will never change. – vojer Apr 25 '17 at 22:45

2 Answers2

1

Good discussion in the comments. My thoughts, based on that discussion?

With an understanding that the question is subjective, you place your constraints:

  • The earliest in a view controller's life cycle where they work.
  • As "close" to the view as possible.
  • If it's something common, make it as universal as possible.
  • Understand how your specifics fit into everything.

(Understand, the question isn't limited to constraints. It could apply to hierarchies, UI, even database tables when you get down to it!)

Sticking to constraints, and my answer....

(1) Use the UIViewController and UIView lifecycles.

Generally the view life cycle is loadView, viewDidLoad, viewWillAppear, viewWillLayoutSubviews, viewDidLayoutSubviews, and viewDidAppear. great SO answer detailing this.

I believe that loadView is too early for constraints, but not viewDidLoad - **provided you aren't expecting to know the frame size. While many say viewDidLayoutSubviews is the right place for that, I've found that viewWillLayoutSubviews most times works just as well. Either way, get your constraints set as soon as possible!

(2) Do it as close to the view as possible.

If you have subviews - I have a "ToolBar" class of objects - you want the constraints, at least as much as possible, to be coded inside the class. For instance, in my tool bar, it slides out, has buttons, and even rotates upon orientation. The only constraints not inside these classes is for orientation - that owner is (and I believe should be) the view controller instantiating it.

(3) Make it universal.

I plan to use this tool bar across a few apps. So the first thing I did was add it to a framework. This framework was needed because I had an app that I delivered a photo editing exension - and the "edit" screen is as much the same as possible. In the end I move all my constraints there. (At least as much as possible.) Anything that I believe is reusable.

(4) Understand the specific requirements of your app.

This should be obvious. If you need to code for various orientations, use arrays and activate/deactivate them. (YES, a common mistake is replacing them! That's setting yourself up for some major headaches.)

If you can keep things active, declare the constraint, set `isActive = true1, and forget about it. If you need to adjust that constraint's constant or multiplier, in the declaration name it and then where you need to alter it, do it.

My conclusion? Auto layout is a very useful tool - more so in code. But the placement of code is like asking "how does one code an OOP app for auto rentals" or " how does one design a database for auto rentals". It not just an art, there are many answers. These are the "rules" I try to follow - YMMV.

Community
  • 1
  • 1
1

To get started with this style of development I recommend checking out Let's Build That App as he goes through very in-depth examples of setting up complex apps entirely in code, without storyboards.

The way he structures the constraints is using a custom implementation of UIView, that way your view code is separated from the ViewController. Then, in the viewDidLoad method you can instantiate your implementation of UIView with something like self.view = MyView().

I wrote a few apps like this. The major drawbacks are that it can become very difficult to make quick adjustments, and you really need to learn about all the different types of constraints you can use.

Here's a pastebin of some extensions I used when doing this. I hope this helps.

Forest Kunecke
  • 2,160
  • 15
  • 32