具有自动布局的iOS 7 UITableViewCell中的约束冲突 [英] Conflicting constraints in iOS 7 UITableViewCell with AutoLayout

查看:71
本文介绍了具有自动布局的iOS 7 UITableViewCell中的约束冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我在单独的XIB文件中创建一个空的UITableViewCell. 层次结构如下所示: https://www.dropbox.com/s/nba8e5t7dvti7os/hierarchy.png

Basically, I create an empty UITableViewCell in separate XIB file. Hierarchy is shown here: https://www.dropbox.com/s/nba8e5t7dvti7os/hierarchy.png

我想在运行时基于内容(通过更新约束常量)并基于此计算单元格/行高度来更改内部视图的高度.因此,如果我有很高的内容,我的行会扩展,如果我有简短的内容,我的行会变小.我想使用自动版式进行这些计算.

I want to change height of my inner view at runtime based on content (by updating constraint constant) and based on this calculate cell/row height. So, if I have tall content, my row expands, if I have short content, my row becomes smaller. I want to use AutoLayout for these calculations.

我有约束要坚持在ContentView的顶部/底部,并且希望我的内部视图的Height = 20.

I have constraints to stick to top/bottom of ContentView and want my inner view to have Height =20.

几乎立即我看到了冲突: https://www.dropbox.com/s/b3effgdw3t2768m/conflict.png

And almost immediately I see a conflict: https://www.dropbox.com/s/b3effgdw3t2768m/conflict.png

我肯定对AutoLayout的理解还不完整.

Most definitely my understanding of AutoLayout is not complete.

第一个问题是:我希望视图的高度为20pt,ContenView的尺寸完全相同怎么办?

1st question is: how is that wrong that I want my view to be 20pt height and my ContenView to be exactly the same size?

第二个问题是:我该如何实现?

2nd question is: how do I achieve that?

推荐答案

这是我专门为您破解的演示应用程序,完全是用代码完成的.

Here's a demo app I've hacked up for you, done purely with code.

您会在我的DemoTableViewCell.m文件中看到,我使用的约束没有固定的高度约束,可以使内容自由扩展.它固定在四个contentView边缘(顶部,右侧,底部,左侧),并具有可选的20像素填充以用于可视化格式.

You'll see in my DemoTableViewCell.m file, the constraints I've used doesn't have a fixed height constraint, to allow the content to freely expand. It's pinned to the four contentView edges (top, right, bottom, left) with an optional 20 pixel padding for visual formatting purpose.

结果是这样的:

#import "AppDelegate.h"
#import "DemoViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    DemoViewController *demoVC = [[DemoViewController alloc] init];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:demoVC];

    self.window.rootViewController = navController;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

DemoViewController.h

#import <UIKit/UIKit.h>

@interface DemoViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *arrText;
@property (nonatomic, strong) UITableView *tableView;

@end

DemoViewController.m

#import "DemoViewController.h"
#import "DemoTableViewCell.h"

@interface DemoViewController ()
{
    DemoTableViewCell *offscreenCell;
}

@end

@implementation DemoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.title = @"Dynamic Cell Height";


    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];

    // init data source

    self.arrText = @[
                     @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",

                     @"Cras rhoncus velit odio, quis dapibus orci tristique eget. Aenean pretium lorem sit amet risus euismod, at dictum justo imperdiet. Suspendisse id feugiat quam, quis mattis felis. Maecenas justo magna, pellentesque sed elementum sed, ultricies rutrum neque.",

                     @"Cras lobortis lacus arcu, non ullamcorper magna faucibus eu. Donec adipiscing eu odio ac dignissim.",

                     @"Vivamus sodales leo massa, in rhoncus purus placerat sit amet. Praesent mattis, lacus scelerisque porttitor interdum, urna sapien ullamcorper est, in placerat augue nulla eget turpis. Curabitur et feugiat orci, lacinia commodo nisl. Nulla sit amet tellus consequat, luctus tellus nec, porttitor libero. Proin mattis, purus nec pellentesque mollis, ligula sapien vehicula magna, et volutpat ligula ligula pulvinar risus. Cras hendrerit urna sagittis iaculis scelerisque. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse ut porttitor arcu. Curabitur sit amet sodales tortor. Pellentesque consequat dui quis mi luctus consectetur. Etiam et dignissim felis.",

                     @"Pellentesque habitant",

                     @"Sed nec consectetur lectus. Mauris at enim nec purus sagittis pellentesque eget id urna."
                     ];

    // init table view

    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    self.tableView.separatorInset = UIEdgeInsetsZero;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    [self.view addSubview:self.tableView];
}

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

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

#pragma mark - UITableViewDelegate Methods -

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";

    DemoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if(cell == nil)
    {
        cell = [[DemoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    cell = [self setupCell:cell forIndexPath:indexPath];

    return cell;
}

// ---------------------------------------------------------------------------
// This method will setup the cell text and such
// ---------------------------------------------------------------------------
-(DemoTableViewCell *)setupCell:(DemoTableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
    cell.lblText.text = [self.arrText objectAtIndex:indexPath.row];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(offscreenCell == nil)
    {
        offscreenCell = [[DemoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"offscreenCell"];
    }

    DemoTableViewCell *cell = [self setupCell:offscreenCell forIndexPath:indexPath];

    [cell layoutIfNeeded];

    return [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
}

@end

DemoTableViewCell.h

#import <UIKit/UIKit.h>

@interface DemoTableViewCell : UITableViewCell

@property (nonatomic, strong) UILabel *lblText;

@end

DemoTableViewCell.m

#import "DemoTableViewCell.h"

@implementation DemoTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

        [self initView];
        [self addAllConstraints];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

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

    // Configure the view for the selected state
}

-(void)initView
{
    self.lblText = [[UILabel alloc] init];
    self.lblText.numberOfLines = 0;
    self.lblText.lineBreakMode = NSLineBreakByWordWrapping;
    self.lblText.preferredMaxLayoutWidth = self.bounds.size.width - 40.0;
    self.lblText.font = [UIFont systemFontOfSize:12];

    [self.contentView addSubview:self.lblText];
}

-(void)addAllConstraints
{
    self.lblText.translatesAutoresizingMaskIntoConstraints = NO;

    id views = @{@"lblText": self.lblText};

    // ------------------------------------------------------------
    // Pin the label text subview to all four contentView edges.
    //
    // The lblText should automatically expand to fit the content
    // text, no need for fixed height constraints here.
    // ------------------------------------------------------------
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[lblText]-20-|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[lblText]-20-|" options:0 metrics:nil views:views]];
}

@end

这篇关于具有自动布局的iOS 7 UITableViewCell中的约束冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆