美文网首页iOS开发实用技术
Masonry配合使用Scrollview

Masonry配合使用Scrollview

作者: Rokkia | 来源:发表于2017-03-03 17:10 被阅读142次
//
//  HomepageViewController.m
//  ZitoProject
//
//  Created by mac on 17/3/1.
//  Copyright © 2017年 x5. All rights reserved.
//

#import "HomepageViewController.h"
#import "ProductDetailViewController.h"

@interface HomepageViewController ()<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,strong)NSArray *shortarray;
@property(nonatomic,strong)NSArray *longArray;
@property(nonatomic,assign)BOOL isLong;
//@property(nonatomic,strong)UIScrollView *scrollview;
//@property(nonatomic,strong)UIScrollView *scrollview;
@end

@implementation HomepageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isLong = NO;
    [self setupView];
    
}

-(void)setupView{
    UIScrollView *scrollview = [[UIScrollView alloc]init];
    [self.view addSubview:scrollview];
    [scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view);
    }];
    //关键  要添加一个container作为contentview
    UIView *container = [UIView new];
    [scrollview addSubview:container];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollview);
        //这里的值是可以修改的,在修改本处的同时 需要连同下面的第一个创建的view的left right height top一同修改
        make.width.equalTo(scrollview);
        make.height.greaterThanOrEqualTo(@0.f);//此处保证容器View高度的动态变化 大于等于0.f的高度
    }];
    
    UILabel *lb = [[UILabel alloc]init];
    [container addSubview:lb];
    [lb mas_makeConstraints:^(MASConstraintMaker *make) {
        //一定要设置好view与container的约束
        //根据上面的修改来修改此处
        make.left.mas_equalTo(container);
        make.right.mas_equalTo(container);
        make.height.mas_equalTo(200);
        make.top.mas_equalTo(container);
    }];
    lb.numberOfLines = 0;
    lb.text = @"hahahahhahahahahahhahahahahahhahaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    
    self.tbView = [[UITableView alloc]init];
    self.tbView.scrollEnabled = NO;
    [container addSubview:_tbView];
    [_tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lb.mas_bottom);
        make.height.mas_equalTo(300);
        make.left.right.mas_equalTo(container);
    }];
    _tbView.estimatedRowHeight = 30;
    _tbView.rowHeight = UITableViewAutomaticDimension;
    _tbView.delegate = self;
    _tbView.dataSource = self;
    [_tbView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.tbView);
    }];

}

#pragma mark - tableViewDelegate datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = _isLong ? self.longArray[indexPath.row] : self.shortarray[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)clickbtn:(id)sender {
    self.isLong = !self.isLong;
    [self.tbView reloadData];
    CGSize size = [self getTbViewContentSize];
    NSLog(@"%f",size.height);
    [self.tbView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(size.height);
    }];
}

-(CGSize)getTbViewContentSize{
    [self.tbView layoutIfNeeded];
    return self.tbView.contentSize;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (NSArray *)longArray {
    if(_longArray == nil) {
        _longArray = @[
                       @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                       ];
    }
    return _longArray;
}

- (NSArray *)shortarray {
    if(_shortarray == nil) {
        _shortarray = @[
                        @"aaaaa",
                        @"aaaaa",
                        @"aaaaa",
                        @"aaaaa",
                        @"aaaaa",
                        @"aaaaa",
                        @"aaaaa",
                        @"aaaaa",
                        @"aaaaa",
                        @"aaaaa",];
    }
    return _shortarray;
}

@end

相关文章

网友评论

    本文标题:Masonry配合使用Scrollview

    本文链接:https://www.haomeiwen.com/subject/ahsrgttx.html