MASExampleBasicView.m 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 *view1 = UIView.new;
  14. view1.backgroundColor = UIColor.greenColor;
  15. view1.layer.borderColor = UIColor.blackColor.CGColor;
  16. view1.layer.borderWidth = 2;
  17. [self addSubview:view1];
  18. UIView *view2 = UIView.new;
  19. view2.backgroundColor = UIColor.redColor;
  20. view2.layer.borderColor = UIColor.blackColor.CGColor;
  21. view2.layer.borderWidth = 2;
  22. [self addSubview:view2];
  23. UIView *view3 = UIView.new;
  24. view3.backgroundColor = UIColor.blueColor;
  25. view3.layer.borderColor = UIColor.blackColor.CGColor;
  26. view3.layer.borderWidth = 2;
  27. [self addSubview:view3];
  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. [view1 makeConstraints:^(MASConstraintMaker *make) {
  33. make.top.greaterThanOrEqualTo(superview.top).offset(padding);
  34. make.left.equalTo(superview.left).offset(padding);
  35. make.bottom.equalTo(view3.top).offset(-padding);
  36. make.right.equalTo(view2.left).offset(-padding);
  37. make.width.equalTo(view2.width);
  38. make.height.equalTo(view2.height);
  39. make.height.equalTo(view3.height);
  40. }];
  41. //with is semantic and option
  42. [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.top.equalTo(superview.mas_top).with.offset(padding); //with with
  44. make.left.equalTo(view1.mas_right).offset(padding); //without with
  45. make.bottom.equalTo(view3.mas_top).offset(-padding);
  46. make.right.equalTo(superview.mas_right).offset(-padding);
  47. make.width.equalTo(view1.mas_width);
  48. make.height.equalTo(@[view1, view3]); //can pass array of views
  49. }];
  50. [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.top.equalTo(view1.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(@[view1.mas_height, view2.mas_height]); //can pass array of attributes
  56. }];
  57. return self;
  58. }
  59. @end