UIView+FLEX_Layout.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // UIView+FLEX_Layout.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 7/18/19.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "UIView+FLEX_Layout.h"
  9. @implementation UIView (darkMode)
  10. #pragma clang diagnostic push
  11. #pragma clang diagnostic ignored "-Wunguarded-availability-new"
  12. #pragma clang diagnostic ignored "-Wunguarded-availability"
  13. - (BOOL)darkMode {
  14. if ([[self traitCollection] respondsToSelector:@selector(userInterfaceStyle)]){
  15. return ([[self traitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark);
  16. } else {
  17. return false;
  18. }
  19. return false;
  20. }
  21. #pragma clang diagnostic pop
  22. @end
  23. @implementation UIView (FLEX_Layout)
  24. - (void)flex_centerInView:(UIView *)view {
  25. [NSLayoutConstraint activateConstraints:@[
  26. [self.centerXAnchor constraintEqualToAnchor:view.centerXAnchor],
  27. [self.centerYAnchor constraintEqualToAnchor:view.centerYAnchor],
  28. ]];
  29. }
  30. - (void)flex_pinEdgesTo:(UIView *)view {
  31. [NSLayoutConstraint activateConstraints:@[
  32. [self.topAnchor constraintEqualToAnchor:view.topAnchor],
  33. [self.leftAnchor constraintEqualToAnchor:view.leftAnchor],
  34. [self.bottomAnchor constraintEqualToAnchor:view.bottomAnchor],
  35. [self.rightAnchor constraintEqualToAnchor:view.rightAnchor],
  36. ]];
  37. }
  38. - (void)flex_pinEdgesTo:(UIView *)view withInsets:(UIEdgeInsets)i {
  39. [NSLayoutConstraint activateConstraints:@[
  40. [self.topAnchor constraintEqualToAnchor:view.topAnchor constant:i.top],
  41. [self.leftAnchor constraintEqualToAnchor:view.leftAnchor constant:i.left],
  42. [self.bottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:-i.bottom],
  43. [self.rightAnchor constraintEqualToAnchor:view.rightAnchor constant:-i.right],
  44. ]];
  45. }
  46. - (void)flex_pinEdgesToSuperview {
  47. [self flex_pinEdgesTo:self.superview];
  48. }
  49. - (void)flex_pinEdgesToSuperviewWithInsets:(UIEdgeInsets)insets {
  50. [self flex_pinEdgesTo:self.superview withInsets:insets];
  51. }
  52. - (void)flex_pinEdgesToSuperviewWithInsets:(UIEdgeInsets)i aboveView:(UIView *)sibling {
  53. UIView *view = self.superview;
  54. [NSLayoutConstraint activateConstraints:@[
  55. [self.topAnchor constraintEqualToAnchor:view.topAnchor constant:i.top],
  56. [self.leftAnchor constraintEqualToAnchor:view.leftAnchor constant:i.left],
  57. [self.bottomAnchor constraintEqualToAnchor:sibling.topAnchor constant:-i.bottom],
  58. [self.rightAnchor constraintEqualToAnchor:view.rightAnchor constant:-i.right],
  59. ]];
  60. }
  61. - (void)flex_pinEdgesToSuperviewWithInsets:(UIEdgeInsets)i belowView:(UIView *)sibling {
  62. UIView *view = self.superview;
  63. [NSLayoutConstraint activateConstraints:@[
  64. [self.topAnchor constraintEqualToAnchor:sibling.bottomAnchor constant:i.top],
  65. [self.leftAnchor constraintEqualToAnchor:view.leftAnchor constant:i.left],
  66. [self.bottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:-i.bottom],
  67. [self.rightAnchor constraintEqualToAnchor:view.rightAnchor constant:-i.right],
  68. ]];
  69. }
  70. @end