Skip to content

Instantly share code, notes, and snippets.

@bobmoff
Last active November 27, 2021 06:58
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save bobmoff/5276954 to your computer and use it in GitHub Desktop.
Save bobmoff/5276954 to your computer and use it in GitHub Desktop.
Simple but really useful category on UIView that makes modifying the frame NOT hellish. Published under WTFPL [http://www.wtfpl.net/]. Usage example can be found in the comments
/*
Before:
CGRect frame = myView.frame;
frame.origin.x = newX;
myView.frame = frame;
After:
myView.x = newX;
*/
#import <UIKit/UIKit.h>
@interface UIView (Ext)
@property float x;
@property float y;
@property float width;
@property float height;
@end
#import "UIView+Ext.h"
@implementation UIView (Ext)
-(float) x {
return self.frame.origin.x;
}
-(void) setX:(float) newX {
CGRect frame = self.frame;
frame.origin.x = newX;
self.frame = frame;
}
-(float) y {
return self.frame.origin.y;
}
-(void) setY:(float) newY {
CGRect frame = self.frame;
frame.origin.y = newY;
self.frame = frame;
}
-(float) width {
return self.frame.size.width;
}
-(void) setWidth:(float) newWidth {
CGRect frame = self.frame;
frame.size.width = newWidth;
self.frame = frame;
}
-(float) height {
return self.frame.size.height;
}
-(void) setHeight:(float) newHeight {
CGRect frame = self.frame;
frame.size.height = newHeight;
self.frame = frame;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment