Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @implementation UIView (Positioning)
- // Add this to a category on UIView. (Remember to add as a subview on another view BEFORE calling this method!)
- - (void)centerOnSuperviewHorizontally:(BOOL)horizontally vertically:(BOOL)vertically {
- NSAssert(self.superview, @"Cannot center a view on its superview if the view has no superview.");
- CGSize selfSize = self.frame.size;
- CGSize superviewSize = self.superview.frame.size;
- CGPoint origin = self.frame.origin;
- if (horizontally) origin.x = (superviewSize.width - selfSize.width)/2;
- if (vertically) origin.y = (superviewSize.height - selfSize.height)/2;
- CGRect frame = self.frame;
- frame.origin = origin;
- self.frame = frame;
- }
- @end
Add Comment
Please, Sign In to add comment