Guest User

Untitled

a guest
Jan 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. @implementation UIView (Positioning)
  2.  
  3. // Add this to a category on UIView. (Remember to add as a subview on another view BEFORE calling this method!)
  4. - (void)centerOnSuperviewHorizontally:(BOOL)horizontally vertically:(BOOL)vertically {
  5. NSAssert(self.superview, @"Cannot center a view on its superview if the view has no superview.");
  6. CGSize selfSize = self.frame.size;
  7. CGSize superviewSize = self.superview.frame.size;
  8. CGPoint origin = self.frame.origin;
  9. if (horizontally) origin.x = (superviewSize.width - selfSize.width)/2;
  10. if (vertically) origin.y = (superviewSize.height - selfSize.height)/2;
  11. CGRect frame = self.frame;
  12. frame.origin = origin;
  13. self.frame = frame;
  14. }
  15.  
  16. @end
Add Comment
Please, Sign In to add comment