Geo*_*rge 5 uitableview cliptobounds ios7
我和其他人一样遇到了与 UITableViewCell 裁剪到其边界相同的问题。正如其他人正确指出的那样,单元格的 contentView 有一个新的超级视图。所以我这样做了:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"HomeTableViewCell";
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
{
cell = [[ItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//add views here as subviews to cell.contentView. Set them tags and retrieve later.
}
cell.clipsToBounds = FALSE;
cell.layer.masksToBounds = FALSE;
cell.contentView.clipsToBounds = FALSE;
cell.contentView.layer.masksToBounds = FALSE;
cell.contentView.superview.clipsToBounds = FALSE;
cell.contentView.superview.layer.masksToBounds = FALSE;
[self loadCell:cell inTable:tableView atIndexPath:indexPath];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
loadCell: inTable: atIndexPath:
通过标签检索单元格的子视图并加载正确的内容。效果很好。没有其他提及clipToBounds
或layer.masksToBounds
。但是:当我重新加载选定的单元格时,它会暂时剪辑到边界,然后它会点击上面的代码并得到正确的结果。因此,在大约 1-2 秒的时间里,我剪掉了细胞。这就是里面发生的事情ItemCell
。我刚刚创建这个类是为了看看是什么将该clipToBounds
属性变为 TRUE。
@implementation ItemCell
- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
{
[self addObserver:self forKeyPath:@"layer.masksToBounds" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"clipsToBounds" options:NSKeyValueObservingOptionNew context:nil];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSNumber *nr = [change objectForKey:@"new"];
if(nr.intValue)
{
self.clipsToBounds = FALSE; // STAY FALSE!!
self.layer.masksToBounds = FALSE;
}
}
Run Code Online (Sandbox Code Playgroud)
即使当它们变为 TRUE 时我将它们设置为 FALSE ,我仍然会得到剪辑/未剪辑之间的差距。只是比较小而已。iOS6 上并没有发生这种情况。clipsToBounds
这是当属性变为 TRUE时的堆栈跟踪,您可以看到它CALayer setMasksToBounds
被自动调用:
有人知道有什么解决办法吗?谢谢你!
我现在似乎已经在我的代码中解决了这个问题。首先确保在 Interface Builder 中设置了有关剪切等的所有内容。然后,以下代码的全部或部分组合似乎是必要的:
在tableView:cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
//ios7 hacks
[cell.contentView.superview setClipsToBounds:NO];
[cell.contentView setClipsToBounds:NO];
[cell setClipsToBounds:NO];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
在tableView:willDisplayCell:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//ios 7 cell background fix
[cell.contentView.superview setClipsToBounds:NO];
[cell.contentView setClipsToBounds:NO];
[cell setClipsToBounds:NO];
[cell setBackgroundColor:[UIColor clearColor]];
//ios 7 content clipping fix
[cell.contentView.superview setClipsToBounds:NO];
[cell.contentView setClipsToBounds:NO];
[cell setClipsToBounds:NO];
}
Run Code Online (Sandbox Code Playgroud)