Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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
iOS7&x27;如果超出父视图边界,则修剪子视图_Ios_Objective C_Uitableview_Ios7 - Fatal编程技术网

iOS7&x27;如果超出父视图边界,则修剪子视图

iOS7&x27;如果超出父视图边界,则修剪子视图,ios,objective-c,uitableview,ios7,Ios,Objective C,Uitableview,Ios7,我已经翻阅了来自苹果的新用户界面信息——没有帮助 现在让代码和屏幕截图向您展示我遇到的问题。 为了确保这不是我的错误代码,我创建了一个新项目,只有一个文件——一个UIViewController,它的id中有一个tableView。委托已设置 我做了以下工作: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. retu

我已经翻阅了来自苹果的新用户界面信息——没有帮助

现在让代码和屏幕截图向您展示我遇到的问题。 为了确保这不是我的错误代码,我创建了一个新项目,只有一个文件——一个UIViewController,它的id中有一个tableView。委托已设置

我做了以下工作:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
    // Configure the cell...

    UIView * redView = [[UIView alloc] initWithFrame:CGRectMake(0, -10, 100, 20)];
    redView.backgroundColor = [UIColor redColor];
    [cell addSubview:redView];

    return cell;
}
表格视图设置为“分组”。 让我们在iOS 6上运行它:

当然,Y原点是负数! 是的,是的,这就是我想要达到的结果。 让我们看看它在iOS 7上显示了什么:

提示:如果我们将redView添加到普通UIView,则不会出现这种情况

另一个提示:如果我将tableView的背景颜色设置为蓝色,则各部分之间的灰线将为蓝色,而不是灰色(以证明灰色不是设置的标题)。 另一个提示:ipad也是如此


为什么在iOS 7中,它会删除所有超出范围的内容?请帮忙

以下内容将解决此问题:

cell.layer.masksToBounds=NO

但是,它可能会破坏其他内容,例如细胞动画

您遇到的问题是由于单元格不支持在其边界之外绘制内容(实际上,使用扩展其superview边界的子视图始终是一种不成熟的解决方案)

最好的建议是重新设计并避免这种功能性


另一个解决方案是将相同的视图添加到上一个单元格的底部,或者直接将它们添加为
UITableView
的子视图。

我认为内容实际上并不是在
masksToBounds
意义上被修剪的,而是视图被其他(不透明)单元格覆盖。您可以尝试通过更改
UITableView
子视图的顺序(“z-Index”)来修复它,每当单元格出现时,使用
将SubViewToFront:
和类似的方法确保最靠近屏幕底部边缘的单元格是所有单元格的最前面视图。

这是因为iOS 7对UITableViewCells的视图层次结构进行了一些更改

它以前是
UITableViewCell-view->contentView

现在它更像是
uitableview单元格视图->滚动视图->内容视图

解决方案是在scrollView上设置
clipsToBounds=NO
(默认设置为YES)。实现这一点的方法是通过
superview
属性

因此,基本上在iOS6和之前的版本中,为了允许内容溢出单元格边界,您可以执行以下操作:

self.clipsToBounds = NO;                        //cell's view
self.contentView.clipsToBounds = NO;            //contentView
在iOS7中,您还必须防止scrollview不剪切,以便执行以下操作:

self.clipsToBounds = NO;                        //cell's view
self.contentView.clipsToBounds = NO;            //contentView
self.contentView.superview.clipsToBounds = NO;  //scrollView
我使用的向后兼容解决方案是:

self.clipsToBounds = NO;
self.contentView.clipsToBounds = NO;
if ([self.contentView.superview isKindOfClass:[NSClassFromString(@"UITableViewCellScrollView") class]]) self.contentView.superview.clipsToBounds = NO;

请记住,这是令人讨厌的™ 如果在iOS 8中视图层次结构再次改变,您可能会遇到麻烦。不幸的是,苹果似乎不希望我们将内容从UITableViewCells中泄漏出来,所以看来这是唯一可行的解决方案。

考虑到这是问题所在,我尝试了许多修复方法,但都没有奏效。不过,谢谢你抽出时间@Theo您正确地识别了问题,但不幸的是,
将SubviewToFront
无法解决问题。它只将子视图放在单元格的contentView中,而不是整个tableView中。也就是说,覆盖该单元格的UITableViewCell仍然保留在frontmasktobounds中,根本不起作用,在自定义单元格类init方法和cellforrow tableView数据源方法中都尝试过。。。我正试图遵循您的最佳建议,并通过headerforsection数据源方法正确地执行此操作。但仍在等待,可能有人会针对所描述的情况提出解决方案……您是否尝试将单元格的
clipstobunds
属性设置为否?是的。。没用,不过谢谢你的提示。