1

I'm a pretty new iOS developer. I love AutoLayout and programmatic UI elements. The use of NSLayoutConstraints just makes so much sense to me, and the place it makes sense to adjust their values is in the code.

Yet almost all info I can find online on creating iOS UI elements uses either XIBs or Storyboards. There's lots of talk about when to use one or the other, including here on SO, but I can't seem to find anything about why someone might want to skip BOTH of them and just do it all in code.

Is there something basic that I'm missing here?

For instance, here's a snippet of my code. Can't I just proceed this way without either XIBs or Storyboards?

    UIView *overlayView = [[UIView alloc] initWithFrame:[self getScreenFrameForCurrentOrientation]];
    overlayView.opaque = NO;
    mHighlightImagePicker.cameraOverlayView = overlayView;

    CGSize screenSize = [[UIScreen mainScreen] bounds].size;        
    float cameraAspectRatio = 4.0 / 3.0;
    float imageWidth = floorf(screenSize.width * cameraAspectRatio);
    float scale = ceilf(((screenSize.height - 90) / imageWidth) * 100.0) / 100.0;
    mHighlightImagePicker.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);

    mCameraToolBar=[[UIImageView alloc] init];
    mCameraToolBar.image =[UIImage imageNamed:@"review_bottom_bar"];
    mCameraToolBar.userInteractionEnabled = YES;
    mCameraToolBar.hidden = NO;        
    [mCameraToolBar setTranslatesAutoresizingMaskIntoConstraints:NO];
    [overlayView addConstraint:
     [NSLayoutConstraint constraintWithItem:mCameraToolBar
                                  attribute:NSLayoutAttributeTop
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:overlayView
                                  attribute:NSLayoutAttributeBottom
                                 multiplier:1
                                   constant:-70]];
    [overlayView addConstraint:
     [NSLayoutConstraint constraintWithItem:mCameraToolBar
                                  attribute:NSLayoutAttributeLeft
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:overlayView
                                  attribute:NSLayoutAttributeLeft
                                 multiplier:1
                                   constant:0]];
    [overlayView addConstraint:
     [NSLayoutConstraint constraintWithItem:mCameraToolBar
                                  attribute:NSLayoutAttributeRight
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:overlayView
                                  attribute:NSLayoutAttributeRight
                                 multiplier:1
                                   constant:0]];
    [overlayView addConstraint:
     [NSLayoutConstraint constraintWithItem:mCameraToolBar
                                  attribute:NSLayoutAttributeBottom
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:overlayView
                                  attribute:NSLayoutAttributeBottom
                                 multiplier:1
                                   constant:0]];
    [overlayView addSubview:mCameraToolBar];
3
  • 2
    You definitely can create views programmatically, using loadView. If you google "loadView tutorial" you'll probably get a bunch of relevant hits.
    – Rob
    Commented Jun 5, 2013 at 18:25
  • 1
    Having said that, I think you can be far more productive designing user interfaces with storyboards and NIBs. For new developers, I'd suggest you start with storyboards, using code only where needed. Also, if adding constraints programmatically (i.e. you don't care care about pre iOS 6 support), consider visual formatting language, e.g. @"H:|[mCameraToolBar]|" and @"V:[mCameraToolBar(70)]|".
    – Rob
    Commented Jun 5, 2013 at 18:59
  • 3
    To me all those storyboards and XIBs are just clutter. Having 3 options feels like I have to learn 3x as much to make an educated choice. That said, I always feel comfortable with code, it can give you many more configuration options, and finally I'm just a sucker for statical analysis, which you can forget about using interface builder and the like (i.e. let's match magic strings). From my limited experience, people who advise using storyboards/xibs are either junior developers or corporate developers for whom quantity of views is a productivity indicator.
    – Gleno
    Commented Aug 16, 2013 at 15:32

1 Answer 1

5

You could do it all in code if you really wanted to. However the reason to use .NIB and storyboards is that it is often faster. I personally use a combination of the two. When I am doing something which is easy to do in a storyboard, I use the storyboard. If I need to add something which changes dynamically I often do it programmatically. I think by avoiding using Storyboards and NIB files you end up having to reinvent the wheel and, for me at least, probably take much longer to finish a program then just learning how to use the best tool for any given situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.