Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 具有UITableView节标题的自动布局问题_Ios_Objective C_Uitableview_Autolayout - Fatal编程技术网

Ios 具有UITableView节标题的自动布局问题

Ios 具有UITableView节标题的自动布局问题,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,我正在使用UITableViewController。我有一个项目表,用户可以删除,如果他进入编辑更多。当他进入编辑模式时,我想显示一个标题,该标题提供了删除所有项目的选项。同时,它应该显示一个标签,给出使用空间的信息。如果设备进入横向模式,我希望它能自动调整大小。据我所知,我需要使用自动布局来完成这项工作 我很想在故事板中设计的UIView中设置标题,但是故事板只允许视图控制器,不允许视图。我知道我可以用一个XIB文件保存它,但如果可以的话,我宁愿避免这样做 首先,我已经覆盖了编辑属性,以便在

我正在使用
UITableViewController
。我有一个项目表,用户可以删除,如果他进入编辑更多。当他进入编辑模式时,我想显示一个标题,该标题提供了删除所有项目的选项。同时,它应该显示一个标签,给出使用空间的信息。如果设备进入横向模式,我希望它能自动调整大小。据我所知,我需要使用自动布局来完成这项工作

我很想在故事板中设计的
UIView
中设置标题,但是故事板只允许视图控制器,不允许视图。我知道我可以用一个XIB文件保存它,但如果可以的话,我宁愿避免这样做

首先,我已经覆盖了
编辑
属性,以便在编辑模式下可以重新绘制表格部分

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
}
我使用此代码在适当时插入节标题:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.isEditing)
        return [self headerView];
    else
        return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.isEditing)
        return [self headerView].frame.size.height;
    else
        return 0;
}
魔法发生在
-headerView
方法中。它返回一个
UIView*
,必要时从缓存中获取它。它添加按钮和标签,然后放入约束。我在故事板中使用了这些相同的约束,我没有遇到任何问题

- (UIView *)headerView
{
    if (headerView)
        return headerView;

    float w = [[UIScreen mainScreen] bounds].size.width;

    UIButton *deleteAllButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [deleteAllButton setTitle:@"Delete All" forState:UIControlStateNormal];
    CGRect deleteAllButtonFrame = CGRectMake(8.0, 8.0, 30.0, 30);   // The autolayout should resize this.
    [deleteAllButton setFrame:deleteAllButtonFrame];
    deleteAllButton.translatesAutoresizingMaskIntoConstraints = NO;
    [deleteAllButton setContentHuggingPriority:252 forAxis:UILayoutConstraintAxisHorizontal];
    [deleteAllButton setContentCompressionResistancePriority:751 forAxis:UILayoutConstraintAxisHorizontal];

    CGRect textFrame = CGRectMake(47.0, 8.0, 30.0, 30); // The autolayout should resize this.
    UILabel *currSizeText = [[UILabel alloc] initWithFrame:textFrame];
    currSizeText.text = @"You have a lot of text here telling you that you have stuff to delete";
    currSizeText.translatesAutoresizingMaskIntoConstraints = NO;
    currSizeText.adjustsFontSizeToFitWidth = YES;

    CGRect headerViewFrame = CGRectMake(0, 0, w, 48);
    headerView = [[UIView alloc] initWithFrame:headerViewFrame];
    //headerView.autoresizingMask = UIViewAutoresizingNone;//UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //headerView.translatesAutoresizingMaskIntoConstraints = NO;
    [headerView addSubview:deleteAllButton];
    [headerView addSubview:currSizeText];

    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(deleteAllButton, currSizeText);

    [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[deleteAllButton]-[currSizeText]-|"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:viewsDictionary]];

    [headerView addConstraint:[NSLayoutConstraint constraintWithItem:deleteAllButton
                                      attribute:NSLayoutAttributeHeight
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:headerView
                                      attribute:NSLayoutAttributeHeight
                                     multiplier:0.5
                                       constant:0]];
    [headerView addConstraint:[NSLayoutConstraint constraintWithItem:currSizeText
                                                           attribute:NSLayoutAttributeHeight
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:headerView
                                                           attribute:NSLayoutAttributeHeight
                                                          multiplier:0.5
                                                            constant:0]];
    return headerView;
}
现在,一切都很顺利。按钮保持恒定大小(因为拥抱和压缩阻力高于标签),标签将更改其文本以适应可用空间。当我旋转设备时,它会调整大小。标签上的垂直居中似乎有偏差,但我现在愿意忽略这一点

然而,当我第一次设置节标题时,我得到一个恼人的自动布局警告

2014-02-07 11:25:19.770 ErikApp[10704:70b] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0xb9a4ad0 H:|-(NSSpace(20))-[UIButton:0xb99e220]   (Names: '|':UIView:0xb9a4680 )>",
    "<NSLayoutConstraint:0xb9a4bf0 H:[UIButton:0xb99e220]-(NSSpace(8))-[UILabel:0xb99f530]>",
    "<NSLayoutConstraint:0xb9a4c20 H:[UILabel:0xb99f530]-(NSSpace(20))-|   (Names: '|':UIView:0xb9a4680 )>",
    "<NSAutoresizingMaskLayoutConstraint:0xa2d1680 h=--& v=--& H:[UIView:0xb9a4680(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xb9a4bf0 H:[UIButton:0xb99e220]-(NSSpace(8))-[UILabel:0xb99f530]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

有人对如何消除警告有什么建议吗?

我尝试使用的解决方法是跳过节头内容,直接转到
tableHeaderView
。我已将我的
编辑
属性替换为以下内容:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if (editing)
        self.tableView.tableHeaderView = [self headerView];
    else
        self.tableView.tableHeaderView = nil;
}
它的动画效果不如节标题好,但现在就可以了


这并不能真正解决实际问题(因此称为“解决方案”),所以我不接受这一解决方案。

我在这里为这篇文章创建了一个GitHub回购:

此解决方案实现了一个表头视图,即
self.tableView.tableHeaderView
,而不是具有单个节的表视图的节头

出于测试目的,表标题视图及其子视图被着色。出于测试目的,可选择任意表格标题高度

当表格视图进入编辑模式时,表格标题将延迟实例化并设置动画。当表格视图退出编辑模式时,动画会隐藏表格标题

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
}
通常,在使用“自动布局”时不应设置帧。然而,从某种意义上说,表头是一种特殊情况。不要使用“自动布局”来调整表格标题的大小或位置。相反,您必须设置表格标题的框架(实际上,您只需要设置rect的高度)。反过来,系统将把表头的框架转换为约束

但是,可以在表头的子视图上使用自动布局。其中一些约束安装在表头视图上

@interface ViewController ()

@property (nonatomic, strong) NSArray *mockData;
@property (nonatomic, strong) UIButton *deleteAllButton;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIView *headerView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Fruit";
    self.mockData = @[@"Orange", @"Apple", @"Pear", @"Banana", @"Cantalope"];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (UIButton *)deleteAllButton
{
    if (!_deleteAllButton) {
        _deleteAllButton = [[UIButton alloc] init];
        _deleteAllButton.backgroundColor = [UIColor grayColor];
        [_deleteAllButton setTitle:@"Delete All" forState:UIControlStateNormal];
        _deleteAllButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_deleteAllButton addTarget:self action:@selector(handleDeleteAll) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteAllButton;
}

- (UILabel *)label
{
    if (!_label) {
        _label = [[UILabel alloc] init];
        _label.backgroundColor = [UIColor yellowColor];
        _label.text = @"Delete all button prompt";
        _label.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _label;
}

- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] init];
        // WARNING: do not set translatesAutoresizingMaskIntoConstraints to NO
        _headerView.backgroundColor = [UIColor orangeColor];
        _headerView.clipsToBounds = YES;

        [_headerView addSubview:self.label];
        [_headerView addSubview:self.deleteAllButton];

        [_headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_deleteAllButton]-[_label]-|" options:NSLayoutFormatAlignAllCenterY metrics:0 views:NSDictionaryOfVariableBindings(_label, _deleteAllButton)]];

        [_headerView addConstraint:[NSLayoutConstraint constraintWithItem:self.deleteAllButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_headerView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];
    }

    return _headerView;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    if (self.editing) {
        self.tableView.tableHeaderView = self.headerView;
        [self.tableView layoutIfNeeded];
    }

    [UIView animateWithDuration:1.0 animations:^{

        CGRect rect = self.headerView.frame;

        if (editing) {
            rect.size.height = 60.0f; // arbitrary; for testing purposes
        } else {
            rect.size.height = 0.0f;
        }

        self.headerView.frame = rect;
        self.tableView.tableHeaderView = self.headerView;

        [self.tableView layoutIfNeeded];

    } completion:^(BOOL finished) {

        if (!editing) {
            self.tableView.tableHeaderView = nil;
        }
    }];
}

- (void)handleDeleteAll
{
    NSLog(@"handle delete all");
}

#pragma mark - Table view data source

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = self.mockData[indexPath.row];

    return cell;
}

@end

似乎在重新加载分区时,UITableView在某个时刻同时引用了旧分区标题和新分区标题。如果是相同的观点,就会出现一些问题。因此,您必须始终提供与
tableView:viewForHeaderInSection:
方法不同的视图

有时,在节标题中显示单个实例是非常有用的。为此,每次要求您输入节标题时,您都需要创建一个新视图,并将自定义视图放入其中,适当地配置约束。下面是一个例子:

@property (strong, nonatomic) UIView *headerContentView;

- (void)viewDidLoad {
    // Create the view, which is to be presented inside the section header
    self.headerContentView = [self loadHeaderContentView];
    // Note that we have to set the following property to NO to prevent the unsatisfiable constraints
    self.headerContentView.translatesAutoresizingMaskIntoConstraints = NO;
}

- (UIView *)loadHeaderContentView {
    // Here you instantiate your custom view from a nib
    // or create it programmatically. Speaking in terms
    // of the OP, it should look like the following. (Note:
    // I have removed all the frame-related code as your are
    // not supposed to deal with frames directly with auto layout.
    // I have also removed the line setting translatesAutoresizingMaskIntoConstraints property
    // to NO of the headerContentView object as we do it explicitly in viewDidLoad.
    UIButton *deleteAllButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [deleteAllButton setTitle:@"Delete All" forState:UIControlStateNormal];
    deleteAllButton.translatesAutoresizingMaskIntoConstraints = NO;
    [deleteAllButton setContentHuggingPriority:252 forAxis:UILayoutConstraintAxisHorizontal];
    [deleteAllButton setContentCompressionResistancePriority:751 forAxis:UILayoutConstraintAxisHorizontal];

    UILabel *currSizeText = [[UILabel alloc] init];
    currSizeText.text = @"You have a lot of text here telling you that you have stuff to delete";
    currSizeText.translatesAutoresizingMaskIntoConstraints = NO;
    currSizeText.adjustsFontSizeToFitWidth = YES;

    UIView *headerContentView = [[UIView alloc] init];
    [headerContentView addSubview:deleteAllButton];
    [headerContentView addSubview:currSizeText];

    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(deleteAllButton, currSizeText);

    // In the original post you used to have an ambigious layout
    // as the Y position of neither button nor label was set.
    // Note passing NSLayoutFormatAlignAllCenterY as an option
    [headerContentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[deleteAllButton]-[currSizeText]-|"
                                                                              options:NSLayoutFormatAlignAllCenterY
                                                                              metrics:nil
                                                                                views:viewsDictionary]];
    [headerContentView addConstraint:[NSLayoutConstraint constraintWithItem:deleteAllButton
                                                                  attribute:NSLayoutAttributeCenterY
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:headerContentView
                                                                  attribute:NSLayoutAttributeCenterY
                                                                 multiplier:1
                                                                   constant:0]];
    // Here setting the heights of the subviews
    [headerContentView addConstraint:[NSLayoutConstraint constraintWithItem:deleteAllButton
                                                           attribute:NSLayoutAttributeHeight
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:headerContentView
                                                           attribute:NSLayoutAttributeHeight
                                                          multiplier:0.5
                                                            constant:0]];
    [headerContentView addConstraint:[NSLayoutConstraint constraintWithItem:currSizeText
                                                           attribute:NSLayoutAttributeHeight
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:headerContentView
                                                           attribute:NSLayoutAttributeHeight
                                                          multiplier:0.5
                                                            constant:0]];
    return headerContentView;
}

- (UIView *)headerView {
    UIView *headerView = [[UIView alloc] init];
    [headerView addSubview:self.headerContentView];

    NSDictionary *views = @{@"headerContentView" : self.headerContentView};
    NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[headerContentView]|" options:0 metrics:nil views:views];
    NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headerContentView]|" options:0 metrics:nil views:views];
    [headerView addConstraints:hConstraints];
    [headerView addConstraints:vConstraints];

    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.isEditing)
        return [self headerView];
    return nil;      
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    // You need to return a concrete value here
    // and not the current height of the header.
    if (self.isEditing)
        return 48;
    return 0;
}

你问这个问题已经很久了,但也许答案对你(或其他人)很有帮助

Autolayout(自动)为整个节标题宽度添加了一个约束(调试输出约束列表中的最后一个)。这当然应该是没有问题的,因为在计算子视图的框架时会考虑宽度。 但有时在计算帧时似乎存在舍入误差

只需将较低优先级添加到其中一个子视图宽度值即可解决此问题:

。@“|-[deleteAllButton(30。0@999)]-[currSizeText]-|“


如果按钮宽度不是恒定的,请使用…deleteAllButton(>=30@999)…仅供参考,您可以将标题视图直接放在故事板中的
UITableViewController
上。只需将一个视图拖到桌面上。这听起来是个好主意@Timothymose。不过我确实试过了。它不像我当前的方法那样具有动画效果,即使它消失了,也会留下空白。您的“UITableView标题”实际上是一个节标题。为了清晰起见,最好将其称为节标题。这很公平。我对问题进行了编辑以反映这一点。仅供参考,当您使用autolayout时,不应手动设置组件的框架。我实际上没有遵循这一行的要点:
self.headerContentView=[self-headerContentView]。这应该是self.headerContentView=[self-headerView]?否,此方法应返回视图,然后在节标题中显示该视图。这取决于您是从nib实例化它还是以编程方式创建它,问题是您只需执行一次,因为这个方法在viewDidLoad方法中被调用。我将把它重命名为更清晰的名称。我已经更新了我的答案,因此现在它包含了您原始帖子中的代码。我尝试了您的新代码,现在我得到了错误:“***在-[UITableView layoutSublayersOfLayer:],/SourceCache/UIKit_Sim/UIKit-2903.23/UIView.m:8540中断言失败”我还更改了您的答案,以修复编译时发生的一些小错误。毕竟更新了我的答案。现在它完全工作了。你知道为什么会有吗