MASExampleDebuggingView.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // MASExampleDebuggingView.m
  3. // Masonry iOS Examples
  4. //
  5. // Created by Jonas Budelmann on 3/08/13.
  6. // Copyright (c) 2013 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "MASExampleDebuggingView.h"
  9. @implementation MASExampleDebuggingView
  10. - (id)init {
  11. self = [super init];
  12. if (!self) return nil;
  13. UIView *greenView = UIView.new;
  14. greenView.backgroundColor = UIColor.greenColor;
  15. greenView.layer.borderColor = UIColor.blackColor.CGColor;
  16. greenView.layer.borderWidth = 2;
  17. [self addSubview:greenView];
  18. UIView *redView = UIView.new;
  19. redView.backgroundColor = UIColor.redColor;
  20. redView.layer.borderColor = UIColor.blackColor.CGColor;
  21. redView.layer.borderWidth = 2;
  22. [self addSubview:redView];
  23. UILabel *blueView = UILabel.new;
  24. blueView.backgroundColor = UIColor.blueColor;
  25. blueView.numberOfLines = 3;
  26. blueView.textAlignment = NSTextAlignmentCenter;
  27. blueView.font = [UIFont systemFontOfSize:24];
  28. blueView.textColor = UIColor.whiteColor;
  29. blueView.text = @"this should look broken! check your console!";
  30. blueView.layer.borderColor = UIColor.blackColor.CGColor;
  31. blueView.layer.borderWidth = 2;
  32. [self addSubview:blueView];
  33. UIView *superview = self;
  34. int padding = 10;
  35. //you can attach debug keys to views like so:
  36. // greenView.mas_key = @"greenView";
  37. // redView.mas_key = @"redView";
  38. // blueView.mas_key = @"blueView";
  39. // superview.mas_key = @"superview";
  40. //OR you can attach keys automagically like so:
  41. MASAttachKeys(greenView, redView, blueView, superview);
  42. [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. //you can also attach debug keys to constaints
  44. make.edges.equalTo(@1).key(@"ConflictingConstraint"); //composite constraint keys will be indexed
  45. make.height.greaterThanOrEqualTo(@5000).key(@"ConstantConstraint");
  46. make.top.equalTo(greenView.mas_bottom).offset(padding);
  47. make.left.equalTo(superview.mas_left).offset(padding);
  48. make.bottom.equalTo(superview.mas_bottom).offset(-padding).key(@"BottomConstraint");
  49. make.right.equalTo(superview.mas_right).offset(-padding);
  50. make.height.equalTo(greenView.mas_height);
  51. make.height.equalTo(redView.mas_height).key(@340954); //anything can be a key
  52. }];
  53. [greenView makeConstraints:^(MASConstraintMaker *make) {
  54. make.top.greaterThanOrEqualTo(superview.top).offset(padding);
  55. make.left.equalTo(superview.left).offset(padding);
  56. make.bottom.equalTo(blueView.top).offset(-padding);
  57. make.right.equalTo(redView.left).offset(-padding);
  58. make.width.equalTo(redView.width);
  59. make.height.equalTo(redView.height);
  60. make.height.equalTo(blueView.height);
  61. }];
  62. //with is semantic and option
  63. [redView mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.top.equalTo(superview.mas_top).with.offset(padding);
  65. make.left.equalTo(greenView.mas_right).offset(padding);
  66. make.bottom.equalTo(blueView.mas_top).offset(-padding);
  67. make.right.equalTo(superview.mas_right).offset(-padding);
  68. make.width.equalTo(greenView.mas_width);
  69. make.height.equalTo(@[greenView, blueView]);
  70. }];
  71. return self;
  72. }
  73. @end