MASExampleAspectFitView.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // MASExampleAspectFitView.m
  3. // Masonry iOS Examples
  4. //
  5. // Created by Michael Koukoullis on 19/01/2015.
  6. // Copyright (c) 2015 Jonas Budelmann. All rights reserved.
  7. //
  8. #import "MASExampleAspectFitView.h"
  9. @interface MASExampleAspectFitView ()
  10. @property UIView *topView;
  11. @property UIView *topInnerView;
  12. @property UIView *bottomView;
  13. @property UIView *bottomInnerView;
  14. @end
  15. @implementation MASExampleAspectFitView
  16. // Designated initializer
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:CGRectZero];
  20. if (self) {
  21. // Create views
  22. self.topView = [[UIView alloc] initWithFrame:CGRectZero];
  23. self.topInnerView = [[UIView alloc] initWithFrame:CGRectZero];
  24. self.bottomView = [[UIView alloc] initWithFrame:CGRectZero];
  25. self.bottomInnerView = [[UIView alloc] initWithFrame:CGRectZero];
  26. // Set background colors
  27. UIColor *blueColor = [UIColor colorWithRed:0.663 green:0.796 blue:0.996 alpha:1];
  28. [self.topView setBackgroundColor:blueColor];
  29. UIColor *lightGreenColor = [UIColor colorWithRed:0.784 green:0.992 blue:0.851 alpha:1];
  30. [self.topInnerView setBackgroundColor:lightGreenColor];
  31. UIColor *pinkColor = [UIColor colorWithRed:0.992 green:0.804 blue:0.941 alpha:1];
  32. [self.bottomView setBackgroundColor:pinkColor];
  33. UIColor *darkGreenColor = [UIColor colorWithRed:0.443 green:0.780 blue:0.337 alpha:1];
  34. [self.bottomInnerView setBackgroundColor:darkGreenColor];
  35. // Layout top and bottom views to each take up half of the window
  36. [self addSubview:self.topView];
  37. [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.left.right.and.top.equalTo(self);
  39. }];
  40. [self addSubview:self.bottomView];
  41. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.left.right.and.bottom.equalTo(self);
  43. make.top.equalTo(self.topView.mas_bottom);
  44. make.height.equalTo(self.topView);
  45. }];
  46. // Inner views are configured for aspect fit with ratio of 3:1
  47. [self.topView addSubview:self.topInnerView];
  48. [self.topInnerView mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.width.equalTo(self.topInnerView.mas_height).multipliedBy(3);
  50. make.width.and.height.lessThanOrEqualTo(self.topView);
  51. make.width.and.height.equalTo(self.topView).with.priorityLow();
  52. make.center.equalTo(self.topView);
  53. }];
  54. [self.bottomView addSubview:self.bottomInnerView];
  55. [self.bottomInnerView mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.height.equalTo(self.bottomInnerView.mas_width).multipliedBy(3);
  57. make.width.and.height.lessThanOrEqualTo(self.bottomView);
  58. make.width.and.height.equalTo(self.bottomView).with.priorityLow();
  59. make.center.equalTo(self.bottomView);
  60. }];
  61. }
  62. return self;
  63. }
  64. @end