365Cocoa

A snippet of Objective-C code per day, 365 days long.
Written by Pieter Omvlee, developer and owner of Bohemian Coding.

Apr 14

Day 42: NSView convenience

For today I want to show a simple extension on NSView I find quite useful for constructing views in code, basically reducing three lines of code to one. View layout programming is always convoluted so the fewer lines we need, the better it is;

- (void)setFrameHeight:(float)height
{
  NSSize s = [self frame].size;
  s.height = height;
  [self setFrameSize:s];
}

- (void)setFrameWidth:(float)width
{
  NSSize s = [self frame].size;
  s.width = width;
  [self setFrameSize:s];
}

- (void)setFrameOriginX:(float)x
{
  NSPoint p = [self frame].origin;
  p.x = x;
  [self setFrameOrigin:p];
}

- (void)setFrameOriginY:(float)y
{
  NSPoint p = [self frame].origin;
  p.y = y;
  [self setFrameOrigin:p];  
}

  1. 365cocoa posted this