Guest User

Untitled

a guest
Jan 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. // The implementation for a category on UIView which centers a view on its superview.
  2. // Make sure this method is only called AFTER the view has been added to its superview.
  3. //
  4. // Created by Jonathan Ellis on 28/09/11
  5. //
  6.  
  7. @implementation UIView (Positioning)
  8.  
  9. - (void)centerOnSuperviewHorizontally:(BOOL)horizontally vertically:(BOOL)vertically {
  10. NSAssert(self.superview, @"Cannot center a view on its superview if the view has no superview.");
  11. CGSize selfSize = self.frame.size;
  12. CGSize superviewSize = self.superview.frame.size;
  13. CGPoint origin = self.frame.origin;
  14. if (horizontally) origin.x = (superviewSize.width - selfSize.width)/2;
  15. if (vertically) origin.y = (superviewSize.height - selfSize.height)/2;
  16. CGRect frame = self.frame;
  17. frame.origin = origin;
  18. self.frame = frame;
  19. }
  20.  
  21. @end
Add Comment
Please, Sign In to add comment