iObjectiveSee

Cocoa and iOS Development

Essential UIView Additions

leave a comment

Where you have UIView *aView, you can now get (and set!) frame properties simply by saying “aView.left” or “aView.width”. There’s nothing new here, we simply ported some nice extensions found in Joe Hewitt’s work.

 

Header info:


@property (nonatomic) CGFloat left;

@property (nonatomic) CGFloat top;

@property (nonatomic) CGFloat right;

@property (nonatomic) CGFloat bottom;

@property (nonatomic) CGFloat width;

@property (nonatomic) CGFloat height;

 

…and the implementation:


- (CGFloat)left {

return self.frame.origin.x;

}

- (void)setLeft:(CGFloat)x {

CGRect frame = self.frame;

frame.origin.x = x;

self.frame = frame;

}

- (CGFloat)top {

return self.frame.origin.y;

}

- (void)setTop:(CGFloat)y {

CGRect frame = self.frame;

frame.origin.y = y;

self.frame = frame;

}

- (CGFloat)right {

return self.frame.origin.x + self.frame.size.width;

}

- (void)setRight:(CGFloat)right {

CGRect frame = self.frame;

frame.origin.x = right - frame.size.width;

self.frame = frame;

}

- (CGFloat)bottom {

return self.frame.origin.y + self.frame.size.height;

}

- (void)setBottom:(CGFloat)bottom {

CGRect frame = self.frame;

frame.origin.y = bottom - frame.size.height;

self.frame = frame;

}

- (CGFloat)width {

return self.frame.size.width;

}

- (void)setWidth:(CGFloat)width {

CGRect frame = self.frame;

frame.size.width = width;

self.frame = frame;

}

- (CGFloat)height {

return self.frame.size.height;

}

- (void)setHeight:(CGFloat)height {

CGRect frame = self.frame;

frame.size.height = height;

self.frame = frame;

}

 

 

Written by Sarah

March 11th, 2012 at 5:23 pm

Posted in Uncategorized

Leave a Reply