NSLayoutConstraint 自动布局,懂的大神看一下我的代码。求教。

路上问路的人 发布于 2014/10/07 10:17
阅读 1K+
收藏 0

代码如下:

------------------------------------------------------------------------------

#import "ViewController.h"
#import "ReviewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *_tab;
    NSArray *dataSource;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createTableview];
    
    dataSource = @[@"Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day; May be back in the past, to oneself the paranoid weird belief disillusionment, these days, my mi***",@"Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day; May be back in the past, to oneself the paranoid weird belief disillusionment, these days, my mi Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day; May be back in the past, to oneself the paranoid weird belief disillusionment, these days, my mi****",@"Age has reached the end of the beginning of a word.****"];
}

//add tableview
- (void)createTableview
{
    
    NSDictionary *metrics = @{@"minPixel":@(1),};
    NSDictionary *views = @{};
    NSString *format = @"";
    NSArray *constraints = @[];
    
    UITableView *tableview = [[UITableView alloc] init];
    tableview.translatesAutoresizingMaskIntoConstraints = NO;
    tableview.delegate = self;
    tableview.dataSource = self;
    [self.view addSubview:tableview];
    _tab = tableview;
    
    format = @"H:|-100-[tableview]-100-|";
    views = NSDictionaryOfVariableBindings(tableview);
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
    [self.view addConstraints:constraints];
    
    format = @"V:|-100-[tableview]-100-|";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
    [self.view addConstraints:constraints];
    
}

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


#pragma mark UITableviewDelegate---

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat height;
    height = 150 ;
    
    CGRect rect = [dataSource[indexPath.row] boundingRectWithSize:CGSizeMake(tableView.frame.size.width - 200,20000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:12.0f] forKey:NSFontAttributeName] context:nil];
    
    height +=rect.size.height;
    
    return height;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    ReviewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if(!cell){
        cell = [[ReviewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.blueLb.text = dataSource[indexPath.row];
    
    return cell;
}

@end

-----------------------------------------------------------------------------------------------------

#import "ReviewCell.h"

@implementation ReviewCell


typedef enum{
    ColorLabelRedLabel,
    ColorLabelGreenLabel,
    ColorLabelBlueLabel,
    ColorLabelOrangeLabel
}ColorLabel;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if(self){
        
        NSDictionary *metrics = @{@"minPixel":@(1),};
        NSDictionary *views = @{};
        NSString *format = @"";
        NSArray *constraints = @[];

        for (int i = 0 ;i < 4;i++) {
            
            UILabel *lb = [[UILabel alloc] init];
            lb.translatesAutoresizingMaskIntoConstraints = NO;
            [self.contentView addSubview:lb];
            
            
            switch (i) {
                case ColorLabelRedLabel:
                {
                    lb.backgroundColor = [UIColor redColor];
                  //  lb.frame = CGRectMake(100,15,568,30);
                    _redLb = lb;
                }
                    break;
                   case ColorLabelGreenLabel:
                {
                    //lb.frame = CGRectMake(100,55,568,30);
                    lb.backgroundColor = [UIColor greenColor];
                    _greenLb = lb;
                }
                    break;
                case ColorLabelBlueLabel:
                {
                    lb.backgroundColor = [UIColor blueColor];
                    lb.numberOfLines = 9999 ;
                    lb.font = [UIFont systemFontOfSize:12.0f];
                    lb.lineBreakMode = NSLineBreakByWordWrapping;
                    _blueLb = lb;
                    _blueLb.preferredMaxLayoutWidth = 768 - 200;
                }
                    break;
                    case ColorLabelOrangeLabel:
                {
                    lb.backgroundColor = [UIColor orangeColor];
                    _orangeLb = lb;
      
                }
                    break;
            }
            
        
        }
        
        //_redLb
        format = @"H:|-100-[_redLb]-100-|";
        views = NSDictionaryOfVariableBindings(_redLb,_greenLb,_blueLb,_orangeLb);
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
        [self.contentView addConstraints:constraints];
        //_greenLb
        format = @"H:|-100-[_greenLb]-100-|";
        views = NSDictionaryOfVariableBindings(_redLb,_greenLb,_blueLb,_orangeLb);
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
        [self.contentView addConstraints:constraints];
        // _blueLb
        format = @"H:|-100-[_blueLb]-100-|";
        views = NSDictionaryOfVariableBindings(_redLb,_greenLb,_blueLb,_orangeLb);
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
        [self.contentView addConstraints:constraints];
         //_orangeLb
        format = @"H:|-100-[_orangeLb]-100-|";
        views = NSDictionaryOfVariableBindings(_redLb,_greenLb,_blueLb,_orangeLb);
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
        [self.contentView addConstraints:constraints];
        
        
//        format = @"V:|-15-[_redLb(30)]-10-[_greenLb(40)]-10-[_blueLb]-10-[_orangeLb(20)]-15-|";
//        constraints =[NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
//        [self.contentView addConstraints:constraints];

        format = @"V:|-15-[_redLb(30)]-10-[_greenLb(40)]-10-[_blueLb]";
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
        [self.contentView addConstraints:constraints];
        
        format = @"V:[_orangeLb(20)]-15-|";
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:views];
        [self.contentView addConstraints:constraints];
        
        
        [self needsUpdateConstraints];
    }
    return self;
}

- (void)setBlueLb:(UILabel *)blueLb
{
    

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

思路是在ReviewCell 中添加约束。然后在- (CGFloat)tableView: (UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath 中改变cell的高度。然后用于拉伸出_blueLb的高度。用于实现可变行高布局。但是结果不是这样。如下:

字体没有显示完全。

加载中
返回顶部
顶部