MASExampleMarginView.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // MASExampleMarginView.m
  3. // Masonry iOS Examples
  4. //
  5. // Created by Craig Siemens on 2015-02-23.
  6. // Copyright (c) 2015 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "MASExampleMarginView.h"
  9. @implementation MASExampleMarginView
  10. - (instancetype)init {
  11. self = [super init];
  12. if (!self) return nil;
  13. UIView *lastView = self;
  14. for (int i = 0; i < 10; i++) {
  15. UIView *view = UIView.new;
  16. view.backgroundColor = [self randomColor];
  17. view.layer.borderColor = UIColor.blackColor.CGColor;
  18. view.layer.borderWidth = 2;
  19. view.layoutMargins = UIEdgeInsetsMake(5, 10, 15, 20);
  20. [self addSubview:view];
  21. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  22. make.top.equalTo(lastView.topMargin);
  23. make.bottom.equalTo(lastView.bottomMargin);
  24. make.left.equalTo(lastView.leftMargin);
  25. make.right.equalTo(lastView.rightMargin);
  26. }];
  27. lastView = view;
  28. }
  29. return self;
  30. }
  31. - (UIColor *)randomColor {
  32. CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
  33. CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
  34. CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
  35. return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
  36. }
  37. @end