Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Layout vs Frame Sizes

So I'm slightly embarrassed to ask this because it seems so rudimentary but ever since beginning iOS development (about a year of self/internet teaching), when not using storyboards, I have relied heavily on frame sizes and used auto layout very sparingly. In Objective C I find myself in situation where I need to use Auto Layout more often but with Swift I can animate a frame's x and y origins, its center, etc... I have never run into a situation where I could not accomplish what I want with frame sizes and pixel locations alone and I have done some fairly complex view work. My question is, what are the benefits of using Auto Layout rather than frame sizes and pixel locations? I would hate to miss out on some great performance benefits due to ignorance and habit. Thanks so much!

like image 987
Matt Clevenger Avatar asked Nov 22 '15 00:11

Matt Clevenger


People also ask

When should you not use Auto Layout?

Everything else in UI is better done in Auto Layout from start. This saves you ton of time later. So the only case when you don't need auto layout is when you intend to make a quick sketch that is sure to be thrown away and you also is out of time.

When should I use Auto Layout Figma?

Auto layout is a property you can add to frames and components. It lets you create designs that grow to fill or shrink to fit, and reflow as their contents change. This is great when you need to add new layers, accommodate longer text strings, or maintain alignment as your designs evolve.

What do you mean by auto Layout?

What is Auto Layout? Auto Layout is a constraint-based layout system designed for building dynamically sized user interfaces. It lets you create user interface layouts that dynamically adapt for all screen sizes without manually setting the frame of every view.


1 Answers

tl;dr Learn how to use Auto Layout – it's a huge time saver for real-world apps!

Long answer:

Finally it is up to you if you want to use features like Auto Layout or Size Classes that Apple provides. The main benefit of those is not really performance in terms of UI rendering in the final product. It is the time you need to invest into thinking about all edge cases if you plan to develop an app that works on different screen sizes and orientations.

Let's say you want to do all the work you need to do to support iPhone 4/4s screen size, iPhone 5/5s screen size, iPhone 6/6s (Plus) screen size and iPad screen size plus Portrait and Landscape modes for all the above screen sizes yourself (here's a good overview): Well, there's nothing wrong with it, just go for it. You would then, of course, need to test things very carefully and also always keep this list up to date – in each app you are using your own solution. If that's for you, do it.

But most people want to get their ideas out without struggling with things like different screen sizes too much. You could of course extract your own logic from one app to use it in all of your apps, and that's exactly what Auto Layout and Screen Sizes are about. Simply put, Apple has done all of the work to make your frame-setting work in different screens for you. You just need to use their new vocabulary (Constraints) to make it work.

In other words: It's an abstraction layer on top of handling with the screen rendering logic directly. If you don't like it, let it go. But if you're planning to do some serious apps that should work on different iPhone/iPad generations and also plan to maintain those however, then, please, learn how to do things with Auto Layout and Size Classes. It will save you and all future maintainers quite some time in development.

A good starting point is the docs. Or a tutorial like this one on raywenderlich.com.

Apple says the following about this difficulty themselves (in the docs linked above):

In many ways, programmatically defining a view’s frame provides the most flexibility and power. When a change occurs, you can literally make any change you want. Yet because you must also manage all the changes yourself, laying out a simple user interface requires a considerable amount of effort to design, debug, and maintain. Creating a truly adaptive user interface increases the difficulty by an order of magnitude.

BTW: There's no difference at all in using Auto Layouts or Frames regarding the programming language: Both Swift and Objective-C support both ways perfectly well. It seems you just didn't find out how to change frames in Obj-C yet. ;)

One more thing: You also don't need to use Storyboards to use Auto Layout. You can use Auto Layout from code. See the docs here. There's even plenty of frameworks trying to make this easier (the original APIs from Apple tend to be not very pretty in many terms), I can recommend SnapKit.

like image 54
Jeehut Avatar answered Sep 23 '22 19:09

Jeehut