iOS7's subview trimmed if out of parent view bounds

iOS7's subview trimmed if out of parent view bounds

bringSubviewToFront problem?


1.

I've rumbled through new UI info from apple - didn't help.

Now let the code and the screenshots show you the problem i've ran into. To ensure that is not my buggy code, i've created a new project, with a single file - a UIViewController that has a tableView inside id. the delegates are set.

I do the following:

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

The table view is set on Grouped. Lets run it on iOS 6: 

Solution:

It's because iOS 7 introduced some changes to the view hierarchy of UITableViewCells.

It used to be UITableViewCell view -> contentView.

Now it's more like UITableViewCell view -> scrollView -> contentView.

The solution is to set clipsToBounds = NO on the scrollView (which is set to YES by default). And the way to achieve that is through the superview property.

So basically in iOS6 and prior, to allow content to spill out of the cell bounds, you would do:

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

In iOS7 you have to also prevent the scrollview from not clipping so you'd do something like:

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

And the backwards compatible solution I use is:

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

Keep in mind this is Hacky™ and if the view hierarchy changes again in iOS 8, you might be in trouble. Unfortunately it seems Apple doesn't want us to spill content out of UITableViewCells so AFAIK this is the only workable solution.


2.

The -[UIView bringSubviewToFront:] method only works for direct children, not grandchildren. Remember that the view hierarchy is a tree, and normally a view only knows about its "parent" (or superview) and its direct "children" (or subviews). You would need to do something like this:

// First, get the view embedding the grandchildview to front.
[self bringSubviewToFront:[grandchildview superview]];
// Now, inside that container view, get the "grandchildview" to front. 
[[grandchildview superview] bringSubviewToFront:grandchildview];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值