Pros and cons of using XIBs and doing views programmatically

女生的网名这么多〃 提交于 2019-12-01 19:38:52

问题


I want to decide if it is better to use XIBs or to designs my views completely using code.

So far I have read that when you design your views on interface builder they are pre-built, so even if they use more memory the user feels everything is faster.

People say doing everything using code is harder but I find it to be just as easy, so I want to know if anyone has experienced some real speed gains when using nibs.

What have been your experiences, advice, etc?

Thanks!


回答1:


You should be able to do both -- there are times when building a view programmatically is better/easier, and times when using a .xib is better/easier. Even if you only ever do things one way, you'll run into code that does it the other, and you'll need to be able to deal with that.

If you don't know how to use IB, then building your views in code is certainly easier. That is why you should learn to use IB. Once you understand IB, it's way, way faster to put together most of the view-based UI your app will likely need. IB helps you line things up, center objects, align base lines, connect controls to their targets and actions, etc. I think it's safe to say that everyone who uses IB effectively experiences "real speed gains when using nibs."




回答2:


You should know how to use both. Performance differences between the two are negligible and should not be the reason that you choose one or the other.

Many people who are new to iOS development have the misconception that nibs (.xib files) are inferior to programmatically creating your UI and that if you use IB you're not a good iOS developer. That view is 100% wrong. IB is created by Apple and in use by Apple's developers to create their own Mac OS X and iOS apps. If IB (as a tool) is good enough to be used by some of the best developers in the world, it's probably good enough for most of us.

In practice I have found that a combination of the two usually fits the bill.

In my own apps I find that .xibs are great for laying out the basics of your views quickly and they allow you to iterate very quickly while giving you a preview of what your view will look like. It's also much easier to use auto layout in a .xib file.

Then when you need to do more advanced things like add fancy animations or move views around that is what IBOutlets are for. Anything that you put into a nib can be referenced through an IBOutlet. This allows you do then programmatically make your view come to life.

Lastly, you should fully understand what a nib (.xib) is doing automagically for you. You should understand what happens when a .xib's objects are unfrozen. There are many resources on the internet to understand .xib files better.

Also, learn how to use .xibs in an encapsulated way. For example, .xibs are crazy useful for things like prototype cells and they allow you to keep your code base modular (much more so than storyboards). Also, you will require less UI code in your view controllers.

Lastly, I always say that people should think of IB/.xibs like jQuery. It's going to save you a lot of time but the best developers still know how to do everything in javascript if they have to.

Good luck and have fun!

TL;DR version

  • Performance is not a consideration when deciding to use .xibs or not.
  • Use .xibs because they give you a preview of the view you are creating and they allow you to quickly iterate
  • In practice most apps will use a combination of both. You will programmatically add animations or move views around but the .xibs will be a starting point
  • Understand fully what happens when the objects in a .xib are unfrozen
  • You'll be more productive but be sure you fully understand what is happening behind the scenes.



回答3:


I would always use XIB files unless there was a reason not to. This allows your views to be maintained easily in the future.

Some reasons for creating the views programmatically might be:

  • A control needs to be resized, repositioned or otherwise altered depending on something else
  • Controls need to be added or removed dynamically

There may be more reasons but there are not too many.

If you programmatically create views when there is no need you make it a lot more difficult for other developers to try to figure out what the view will look like and to change it.




回答4:


If you build your views programmatically, you have control over the loading of elements. e.g. you could use lazy loading, and load secondary buttons, subviews, etc. a fraction of a second after the more important elements, allowing the key parts of the UI to come up faster. You could even animate some elements into position.

If you use IB, you get guides as to proper element spacings and positioning, but you could always copy the coordinates from IB into code if you aren't changing the design that often.

For simple UI elements, you will end up with more lines of code to maintain if you create them programatically.




回答5:


IB and NIBs do a lot to optimise loading/unloading of views, but it is largely oriented to minimising memory usage vs. perceived speed for the user. For example, lazy loading if anything might make the app UI slightly slower, but it should make memory usage lower. This in turn could make overall app performance better on a large application, and is very much encouraged, but it's difficult to define "performance" in a narrow way. It's also difficult to say when you should or should not use IB - there will be some times you're much better off doing it in code.

One often overlooked element to the IB or not debate is development speed, especially if you have multiple developers. On a larger team/project you'll probably have some developer(s) who specialise more in the infrastructure, business logic etc. of the app and some developer(s) who specialise more in the UI. In this case, use of IB will make it easier for them to work independently, which should make overall development more efficient.

I view IB as a core part of the development platform for iOS development. It's not the right solution in every situation but not knowing how to use IB will be a real limiting factor.



来源:https://stackoverflow.com/questions/6332998/pros-and-cons-of-using-xibs-and-doing-views-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!