关闭

UIScrollView中嵌入一个UITableView,使用Masonry来写Autolayout的demo

标签: ios开发uiscrollviewuitableview
1391人阅读 评论(1) 收藏 举报
分类:

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">只有一个viewcontroller,所以直接贴上代码就好</span>


//
//  MainViewController.m
//  scroll_embed_table_text
//
//  Created by 马龙 on 15/7/6.
//  Copyright (c) 2015年 马龙. All rights reserved.
//

#import "MainViewController.h"
#import "masonry.h"

@interface MainViewController ()
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UITableView *tableView;

@property(strong, nonatomic) UIView* subview;
@property(strong, nonatomic) UIView* subview2;
@end

NSInteger subview_height = 50;

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    [self.scrollView setBackgroundColor:[UIColor blackColor]];
    
    self.subview = [[UIView alloc] init];
    self.subview.backgroundColor = [UIColor greenColor];
    [self.scrollView addSubview:self.subview];
    
    self.subview2 = [[UIView alloc] init];
    self.subview2.backgroundColor = [UIColor blueColor];
    [self.scrollView addSubview:self.subview2];
    
    self.tableView = [[UITableView alloc] init];
    [self.scrollView addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.automaticallyAdjustsScrollViewInsets = FALSE;
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.view);
        make.width.equalTo(self.view);
        make.height.equalTo(self.view);
    }];
    
    [self.subview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
        make.height.equalTo(@250);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
        make.top.equalTo(self.subview.mas_bottom);
        make.height.equalTo(@400);
    }];
    
    [self.subview2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView);
        make.top.equalTo(self.tableView.mas_bottom);
        make.width.equalTo(self.scrollView);
        make.height.equalTo(@250);
        make.bottom.equalTo( self.scrollView.mas_bottom);
    }];
    

      // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
}


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

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

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* strID = @"ID";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:strID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
    return cell;
}
/*
#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.
}
*/

@end


比较重要是两点:

1、self.subView2的约束里有一行 

make.bottom.equalTo( self.scrollView.mas_bottom);  这行代码确定了scrollView的ContentSize

2、demo中tableview的高度是确定的。如果高度不确定,那么subview2的高度及位置应该是确定的。否则tableview的高度不能正确的被计算。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16101次
    • 积分:339
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:6篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论