3

I'm currently making apps for iOS, and I had a quick question about making UIViews. In the process of designing a UIView, I was wondering if everything should be based off of the bounds of the rectangle that contains my view.

For example, the one I'm currently working on is designed as a header that only occupies the upper 25% of the screen. Despite this intention, should I still design the code so that if the view were to occupy the entire screen , it would still work?

To provide a scenario, lets say I need to draw a line. Should I just draw it 20 pixels across, or should I always go 30% of the width of my rectangle.

I understand the concept of reusability, but if I'm designing this view only for this particular purpose, is it acceptable to make it somewhat rigid in nature?

James Traimory
  • 513
  • 1
  • 4
  • 8

1 Answers1

3

Designing for change and reusability is always a good practice. However, as you have also realized, it introduces overhead which can sometimes outweigh the benefits of the flexible design.

I would say that it is fine to hard code some values if the view is used only for a particular purpose with a particular size. It is fairly common to create fixed size images for UI components which is just like using fixed size values in your code. Nevertheless, it is a good practice to use constants for all your hard coded values and collect all of these at a centralized place of your code e.g.:

static const CGFloat centerLineWidth = 20.0;

This way you can relatively easily reconfigure your views if something needs to be changed.

Finally, if there is even a slight chance that the view might be used with different sizes, you should go for the flexible design. You can also mix these concepts, e.g. create a view which is designed with flexible width in mind but its height should be a fixed value.

Imre Kelényi
  • 22,113
  • 5
  • 35
  • 45
  • 1
    He says "if I'm designing this view only for this particular purpose", so I agree with hard coding values. A question though, where would you (personally) "collect all of [your hard coded values] at a centralized place" ? – rdurand Nov 22 '12 at 09:43
  • 1
    If these values are only related to a particular view and nothing else depends on them, then you should put them at the beginning of the the .m file defining the view and hiding these from the other parts of your code. In contrast with this, if multiple views use a constant, then you should create separate header file, e.g. MyProjectUiConsts.h and collect the constants there, importing them to all of the related views. – Imre Kelényi Nov 22 '12 at 09:51
  • Thanks, I'll keep the constants in mind! It provides a nice balance between hard-coding and reusability. :) – James Traimory Nov 22 '12 at 19:32