MASExampleBasicView.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // MASExampleBasicView.m
  3. // Masonry
  4. //
  5. // Created by Jonas Budelmann on 21/07/13.
  6. // Copyright (c) 2013 cloudling. All rights reserved.
  7. //
  8. #import "MASExampleBasicView.h"
  9. @implementation MASExampleBasicView
  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. UIView *blueView = UIView.new;
  24. blueView.backgroundColor = UIColor.blueColor;
  25. blueView.layer.borderColor = UIColor.blackColor.CGColor;
  26. blueView.layer.borderWidth = 2;
  27. [self addSubview:blueView];
  28. UIView *superview = self;
  29. int padding = 10;
  30. //if you want to use Masonry without the mas_ prefix
  31. //define MAS_SHORTHAND before importing Masonry.h see Masonry iOS Examples-Prefix.pch
  32. [greenView makeConstraints:^(MASConstraintMaker *make) {
  33. make.top.greaterThanOrEqualTo(superview.top).offset(padding);
  34. make.left.equalTo(superview.left).offset(padding);
  35. make.bottom.equalTo(blueView.top).offset(-padding);
  36. make.right.equalTo(redView.left).offset(-padding);
  37. make.width.equalTo(redView.width);
  38. make.height.equalTo(redView.height);
  39. make.height.equalTo(blueView.height);
  40. }];
  41. //with is semantic and option
  42. [redView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.top.equalTo(superview.mas_top).with.offset(padding); //with with
  44. make.left.equalTo(greenView.mas_right).offset(padding); //without with
  45. make.bottom.equalTo(blueView.mas_top).offset(-padding);
  46. make.right.equalTo(superview.mas_right).offset(-padding);
  47. make.width.equalTo(greenView.mas_width);
  48. make.height.equalTo(@[greenView, blueView]); //can pass array of views
  49. }];
  50. [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.top.equalTo(greenView.mas_bottom).offset(padding);
  52. make.left.equalTo(superview.mas_left).offset(padding);
  53. make.bottom.equalTo(superview.mas_bottom).offset(-padding);
  54. make.right.equalTo(superview.mas_right).offset(-padding);
  55. make.height.equalTo(@[greenView.mas_height, redView.mas_height]); //can pass array of attributes
  56. }];
  57. return self;
  58. }
  59. @end