Skip to content

Instantly share code, notes, and snippets.

@wlue
Last active October 13, 2015 22:38
Show Gist options
  • Save wlue/4266219 to your computer and use it in GitHub Desktop.
Save wlue/4266219 to your computer and use it in GitHub Desktop.
Convenient UIView methods that allow fast mutation of frame size and origin.
//
// UIView+Additions.h
//
// Created by Wen-Hao Lue on 12-01-08.
//
@interface UIView (Additions)
@property (nonatomic, assign) CGFloat frameX;
@property (nonatomic, assign) CGFloat frameY;
@property (nonatomic, assign) CGFloat frameBottom;
@property (nonatomic, assign) CGFloat frameRight;
@property (nonatomic, assign) CGFloat frameWidth;
@property (nonatomic, assign) CGFloat frameHeight;
@property (nonatomic, assign) CGPoint frameOrigin;
@property (nonatomic, assign) CGSize frameSize;
@property (nonatomic, assign) CGFloat boundsWidth;
@property (nonatomic, assign) CGFloat boundsHeight;
@property (nonatomic, assign) CGSize boundsSize;
@end
//
// UIView+Additions.m
//
// Created by Wen-Hao Lue on 12-01-08.
//
#import "UIView+Additions.h"
@implementation UIView (Additions)
#pragma mark - Frame Methods
#pragma mark frameX
- (CGFloat)frameX
{
return self.frame.origin.x;
}
- (void)setFrameX:(CGFloat)frameX
{
self.frame = CGRectMake(frameX, self.frameY, self.frameWidth, self.frameHeight);
}
#pragma mark frameY
- (CGFloat)frameY
{
return self.frame.origin.y;
}
- (void)setFrameY:(CGFloat)frameY
{
self.frame = CGRectMake(self.frameX, frameY, self.frameWidth, self.frameHeight);
}
#pragma mark frameBottom
- (CGFloat)frameBottom
{
return self.frame.origin.y + self.frame.size.height;
}
- (void)setFrameBottom:(CGFloat)frameBottom
{
self.frame = CGRectMake(self.frameX, frameBottom - self.frameHeight, self.frameWidth, self.frameHeight);
}
#pragma mark frameRight
- (CGFloat)frameRight
{
return self.frame.origin.x + self.frame.size.width;
}
- (void)setFrameRight:(CGFloat)frameRight
{
self.frame = CGRectMake(frameRight - self.frameWidth, self.frameY, self.frameWidth, self.frameHeight);
}
#pragma mark frameWidth
- (CGFloat)frameWidth
{
return self.frame.size.width;
}
- (void)setFrameWidth:(CGFloat)frameWidth
{
self.frame = CGRectMake(self.frameX, self.frameY, frameWidth, self.frameHeight);
}
#pragma mark frameHeight
- (CGFloat)frameHeight
{
return self.frame.size.height;
}
- (void)setFrameHeight:(CGFloat)frameHeight
{
self.frame = CGRectMake(self.frameX, self.frameY, self.frameWidth, frameHeight);
}
#pragma mark frameOrigin
- (CGPoint)frameOrigin
{
return self.frame.origin;
}
- (void)setFrameOrigin:(CGPoint)point
{
self.frame = CGRectMake(point.x, point.y, self.frameWidth, self.frameHeight);
}
#pragma mark frameSize
- (CGSize)frameSize
{
return self.frame.size;
}
- (void)setFrameSize:(CGSize)size
{
self.frame = CGRectMake(self.frameX, self.frameY, size.width, size.height);
}
#pragma mark boundsWidth
- (CGFloat)boundsWidth
{
return self.bounds.size.width;
}
- (void)setBoundsWidth:(CGFloat)boundsWidth
{
self.bounds = CGRectMake(0.0f, 0.0f, boundsWidth, self.boundsHeight);
}
#pragma mark boundsHeight
- (CGFloat)boundsHeight
{
return self.bounds.size.height;
}
- (void)setBoundsHeight:(CGFloat)boundsHeight
{
self.bounds = CGRectMake(0.0f, 0.0f, self.boundsWidth, boundsHeight);
}
#pragma mark boundsSize
- (CGSize)boundsSize
{
return self.bounds.size;
}
- (void)setBoundsSize:(CGSize)size
{
self.bounds = CGRectMake(0.0f, 0.0f, size.width, size.height);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment