• iOS 用masonry布局Scrollview的问题,添加在scrollview的子控件约束失效


    1. //
    2. // ViewController.m
    3. // Log
    4. //
    5. // Created by xxzx on 2018/11/23.
    6. // Copyright © 2018 xxzx. All rights reserved.
    7. //
    8. #import "ViewController.h"
    9. #import <Masonry.h>
    10. @interface ViewController ()
    11. // scrollView
    12. @property (nonatomic, strong) UIScrollView *scrollView;
    13. // 约束参照视图,也是容器视图
    14. @property (nonatomic, strong) UIView *contentView;
    15. // 第一个测试view
    16. @property (nonatomic, strong) UIView *oneView;
    17. // 第二个测试view
    18. @property (nonatomic, strong) UIView *twoView;
    19. // 第三个测试view
    20. @property (nonatomic, strong) UIView *threeView;
    21. @end
    22. @implementation ViewController
    23. - (void)viewDidLoad {
    24. [super viewDidLoad];
    25. // 1. 添加scrollView
    26. [self.view addSubview:self.scrollView];
    27. // 2. 添加参照视图
    28. [self.scrollView addSubview:self.contentView];
    29. // 3. 添加第一个测试view
    30. [self.contentView addSubview:self.oneView];
    31. // 4. 添加第二个测试view
    32. [self.contentView addSubview:self.twoView];
    33. // 5. 添加第三个测试view
    34. [self.contentView addSubview:self.threeView];
    35. }
    36. - (void)viewDidLayoutSubviews {
    37. [super viewDidLayoutSubviews];
    38. // 布局子控件
    39. // 设置scrollView约束
    40. [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    41. make.edges.equalTo(self.view);
    42. }];
    43. // 设置参照视图的约束
    44. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    45. make.edges.equalTo(self.scrollView);
    46. make.width.equalTo(self.scrollView);
    47. // make.height.greaterThanOrEqualTo(@0.0f);
    48. }];
    49. // 第一个测试view的约束
    50. [self.oneView mas_makeConstraints:^(MASConstraintMaker *make) {
    51. make.top.equalTo(self.contentView).offset(30);
    52. make.left.equalTo(self.contentView);
    53. make.width.mas_equalTo(200);
    54. make.height.mas_equalTo(300);
    55. }];
    56. // 第二个测试view的约束
    57. [self.twoView mas_makeConstraints:^(MASConstraintMaker *make) {
    58. make.top.equalTo(self.oneView.mas_bottom).offset(50);
    59. make.right.equalTo(self.contentView);
    60. make.width.mas_equalTo(400);
    61. make.height.mas_equalTo(500);
    62. }];
    63. // 第三个测试view的约束
    64. [self.threeView mas_makeConstraints:^(MASConstraintMaker *make) {
    65. make.top.equalTo(self.twoView.mas_bottom).offset(70);
    66. make.left.right.equalTo(self.contentView);
    67. make.height.mas_equalTo(300);
    68. }];
    69. // 最后设置最后一个view的与参照容器view的约束
    70. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    71. make.bottom.equalTo(self.threeView.mas_bottom).offset(-10);
    72. }];
    73. }
    74. #pragma mark - getters
    75. // scrollView
    76. - (UIScrollView *)scrollView {
    77. if (_scrollView == nil) {
    78. _scrollView = [[UIScrollView alloc] init];
    79. _scrollView.backgroundColor = [UIColor grayColor];
    80. }
    81. return _scrollView;
    82. }
    83. // 约束参照视图
    84. - (UIView *)contentView {
    85. if (_contentView == nil) {
    86. _contentView = [[UIView alloc] init];
    87. _contentView.backgroundColor = [UIColor yellowColor];
    88. }
    89. return _contentView;
    90. }
    91. // 第一个测试view
    92. - (UIView *)oneView {
    93. if (_oneView == nil) {
    94. _oneView = [[UIView alloc] init];
    95. _oneView.backgroundColor = [UIColor redColor];
    96. }
    97. return _oneView;
    98. }
    99. // 第二个测试view
    100. - (UIView *)twoView {
    101. if (_twoView == nil) {
    102. _twoView = [[UIView alloc] init];
    103. _twoView.backgroundColor = [UIColor blueColor];
    104. }
    105. return _twoView;
    106. }
    107. // 第三个测试view
    108. - (UIView *)threeView {
    109. if (_threeView == nil) {
    110. _threeView = [[UIView alloc] init];
    111. _threeView.backgroundColor = [UIColor greenColor];
    112. }
    113. return _threeView;
    114. }
    115. @end
    116. 复制代码

  • 相关阅读:
    hash 哈希表
    牛客网:NC26 括号生成
    苹果华为荣耀三家争鸣:谁在开启无边界之战?
    Nodejs模块化
    数字图像处理大作业
    Pytest系列-失败重跑插件pytest-rerunfailures的使用(9)
    django项目实战基于Python实现的衣物捐赠系统
    基于Spring Boot+vue的民宿预定管理系统的设计与实现
    Python Day15 json和文件操作【初级】
    GSN前瞻预处理
  • 原文地址:https://blog.csdn.net/q375537943/article/details/133931055