如果超出父视图边界,则会修剪iOS7的子视图

参见英文答案 > How to stop UITableView from clipping UITableViewCell contents in iOS 72个
我从苹果公司发布了新的UI信息 – 没有帮助.

现在让代码和屏幕截图向您展示我遇到的问题.
为了确保这不是我的错误代码,我创建了一个新项目,只有一个文件一个在id里面有一个tableView的UIViewController.代表们已经确定.

我做了以下事情:

- (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;
}

表视图在Grouped上设置.
让我们在iOS 6上运行它:

当然,Y的起源是否定的!
是的,它是,这是我想要实现的结果.
让我们看看它在iOS 7上显示内容

提示:如果我们将redView添加到普通的UIView中,则不会发生这种情况.

一个提示:如果我将tableView的背景颜色设置为蓝色,则各部分之间的灰线将为蓝色,而不是灰色(作为灰色不是设置标题的演示).
一个提示:同样适用于ipad.

为什么在iOS 7中它会削减一切超出范围的内容?请帮忙!

解决方法

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

它曾经是UITableViewCell视图 – >内容查看.

现在它更像是UITableViewCell视图 – > scrollView – >内容查看.

解决方案是在scrollView上设置clipsToBounds = NO(认情况下设置为YES).实现这一目标的方法是通过superview属性.

因此,基本上在iOS6和之前,为了允许内容溢出单元格范围,您可以:

self.clipsToBounds = NO;                        //cell's view
self.contentView.clipsToBounds = NO;            //contentView

在iOS7中,您还必须防止滚动视图不被剪切,因此您可以执行以下操作:

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;

请记住这是Hacky™,如果在iOS 8中视图层次结构再次发生变化,您可能会遇到麻烦.不幸的是,似乎Apple不希望我们将内容从UITableViewCells中溢出,因此AFAIK这是唯一可行的解​​决方案.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...