解决方案 »

  1.   

    自动布局autolayout 会帮你管理在各个尺寸设备下的显示,前提是你正确设置了各元素的约束。使用自动布局后不需要再调整,因为它就是为解决这种问题而出现的。
      

  2.   

    - (void)setupView
    {
        self.backgroundColor = [UIColor whiteColor];
        self.accessoryType = UITableViewCellAccessoryNone;
        self.accessoryView = nil;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        // Fix for contentView constraint warning
        [self.contentView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
        
        // Create our ImageView
        self.sampleImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.sampleImageView.translatesAutoresizingMaskIntoConstraints = NO;
        self.sampleImageView.contentMode = UIViewContentModeScaleAspectFit;
        self.sampleImageView.layer.borderColor = [UIColor colorWithWhite:210.0/255 alpha:1.0].CGColor;
        self.sampleImageView.layer.borderWidth = 0.5;
        self.sampleImageView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.sampleImageView];
        
        // title label
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.titleLabel.font = [UIFont systemFontOfSize:15];
        self.titleLabel.textColor = [UIColor colorWithRed:50.0/255 green:50.0/255 blue:50.0/255 alpha:1.0];
        self.titleLabel.numberOfLines = 1;
        //self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self.contentView addSubview:self.titleLabel];    
        //discass label
        self.discassLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        self.discassLabel.translatesAutoresizingMaskIntoConstraints = NO;
        self.discassLabel.font = [UIFont systemFontOfSize:12];
        self.discassLabel.textColor = [UIColor grayColor];
        self.discassLabel.numberOfLines = 1;
        [self.contentView addSubview:self.discassLabel];
        
        //discass image view
        self.disImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        self.disImageView.translatesAutoresizingMaskIntoConstraints = NO;
        self.disImageView.contentMode = UIViewContentModeScaleAspectFill;
        self.disImageView.backgroundColor = [UIColor clearColor];
        //self.disImageView.image = [UIImage imageNamed:@"comment.png"];
        [self.contentView addSubview:self.disImageView];
        
        CGFloat width = CGRectGetWidth([UIScreen mainScreen].bounds) - 22;
        CGFloat height = width*18.0/29.0;    // Constrain
        NSDictionary *viewDict = NSDictionaryOfVariableBindings(_sampleImageView,_titleLabel,_disImageView,_discassLabel);
        // Create a dictionary with buffer values
        NSDictionary *metricDict = @{@"sideBuffer" : @11, @"verticalBuffer" : @6, @"midBuffer" : @5, @"imageWidth" : [NSNumber numberWithFloat:width],@"imageHeight" : [NSNumber numberWithFloat:height],@"smallicon" : @15};
        
        // Constrain elements horizontally
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-sideBuffer-[_sampleImageView(imageWidth)]" options:0 metrics:metricDict views:viewDict]];
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_disImageView(smallicon)]-midBuffer-[_discassLabel]-smallicon-|" options:0 metrics:metricDict views:viewDict]];
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-smallicon-[_titleLabel]-(50)-|" options:0 metrics:metricDict views:viewDict]];
        // Constrain elements vertically
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-verticalBuffer-[_sampleImageView(imageHeight)]" options:0 metrics:metricDict views:viewDict]];
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_disImageView(sideBuffer)]" options:0 metrics:metricDict views:viewDict]];
        
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.sampleImageView attribute:NSLayoutAttributeBottom multiplier:1 constant:5]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.disImageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.titleLabel attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.discassLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.titleLabel attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];    
        [self.discassLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
        [self.disImageView setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
        [self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    }
    动态自动布局你可以这样,不懂的方法自己百度....
      

  3.   

    如果是动态布局的话,比如Cell,我一般会把某个需要动态的约束用IBOutlet连接到类里,然后通过修改它的constant值来修改约束