Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The implementation for a category on UIView which centers a view on its superview.
- // Make sure this method is only called AFTER the view has been added to its superview.
- //
- // Created by Jonathan Ellis on 28/09/11
- //
- @implementation UIView (Positioning)
- - (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