将UIView置于所有其他视图之上

16

有人能帮我吗,如何使在代码中创建的UIView置于其他视图之上?其中tableView是父视图,subSelectionView是我的自定义同级视图,我想将其置于tableView之上.. 我有以下代码:

这是我的didSelectRowAtIndexPath:的完整源代码,我在其中以编程方式添加UIView实例..

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    // Execute upon selection
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [[tableView viewWithTag:199]removeFromSuperview];

    // Get the cell size
    CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size;

    // Contact Details Container
    UIView *subSelectionView;
    if(indexPath.row < [contacts count] - 6)
        subSelectionView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, (int)cellSize.width - 20, (int)cellSize.height + 130)];

    subSelectionView.backgroundColor = [UIColor grayColor];
    subSelectionView.layer.borderColor = [UIColor grayColor].CGColor;
    subSelectionView.layer.borderWidth = 1;
    subSelectionView.alpha = 0.9;
    subSelectionView.tag = 199;
    subSelectionView.layer.cornerRadius = 5.0;


    // Contact Name Container
    UIView *contactNameSubSelectionView;
    if(indexPath.row < [contacts count] - 6)
        contactNameSubSelectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (int)cellSize.width - 20, (int)cellSize.height)];

    contactNameSubSelectionView.backgroundColor = [UIColor grayColor];
    contactNameSubSelectionView.alpha = 0.5;

    [subSelectionView addSubview:contactNameSubSelectionView];


    // Contact Name Label
    Contacts *contact = [self.contacts objectAtIndex:indexPath.row];
    NSString *contactName = [NSString stringWithFormat:@"  %@ %@ ", contact.fname, contact.lname];

    UILabel *contactNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, (int)cellSize.width, (int)cellSize.height)];
    contactNameLabel.layer.cornerRadius = 4.0;

    [contactNameLabel setText: contactName];
    [contactNameLabel setTextColor:[UIColor blackColor]];
    [contactNameLabel setFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]]];
    [contactNameSubSelectionView addSubview:(UIView *)contactNameLabel];


    // Buttons
    UIButton *buttonMobile = [[UIButton alloc]initWithFrame:CGRectMake(10, cellSize.height + 10, 30, 30)];
    buttonMobile.layer.borderWidth = 1;
    [buttonMobile setTitle:@"..." forState:UIControlStateNormal];
    [buttonMobile addTarget:self action:@selector(btPressed:) forControlEvents:UIControlStateNormal];

    [subSelectionView addSubview:(UIView *) buttonMobile];



    [[tableView cellForRowAtIndexPath:indexPath]insertSubview:subSelectionView aboveSubview:self.view];   
    [self.parentViewController.view bringSubviewToFront:subSelectionView];


    [tableView reloadData];

}

但是并没有起作用...

下面的图片展示了我的subSelectionView,按钮在其中,而subSelectionView在tableView中。现在,当我点击那个按钮时,问题是我实际上无法点击它。即使存在我的代码,它仍然直接聚焦于tableView。有人可以帮帮我吗?...

enter image description here

6个回答

53

UIView实例有一个名为bringSubviewToFront的方法,您可以使用该方法。

例如:

UIVIew *yourSubView;
[yourMainView addSubView:yourSubView];
[yourMainView bringSubviewToFront:yourSubView];

编辑

在您的情况下,应该像这样:

[[tableView cellForRowAtIndexPath:indexPath] insertSubview:subSelectionView aboveSubview:self.view];
[[tableView cellForRowAtIndexPath:indexPath] bringSubviewToFront:subSelectionView];

请问您能否发布完整的源代码,以及如何创建子视图并添加? - Janak Nirmal
@AldeeMativo:bringSubviewToFront: 只能作用于直接子视图,而不能作用于任何后代视图。你需要把该消息发送给表视图单元格,而不是视图控制器的视图。 - Peter Hosey

8
在Swift中,您可以这样做:
self.view.bringSubviewToFront(theViewToMoveToFront)

1
在 Swift 4.2 中,现在是这样的: view.bringSubviewToFront(theViewToMoveToFront) - Freek Sanders

7
如果我理解你的问题正确,你的问题是你无法在位于tableview之上的新视图中实际点击按钮?当你点击它时,下面的UITableView会响应?
这意味着UIButton没有识别出被点击,并且事件传递到其下方的下一个子视图进行处理。现在,有许多可能导致此类行为的原因-您可能已将用户交互禁用或UIView,它的“Enabled”属性设置为NO,或超出UIView的边界(iOS自动假定如果点击超出边界矩形,则错过了接收器)。此外,如果您的alpha设置为0.0,则会忽略单击。
您应该检查以上所有内容。

谢谢Pete。我已经检查过了并将按钮的enabled属性设置为yes,但仍然无法工作。嗯...我使用这段代码是正确的吗:[[tableView cellForRowAtIndexPath:indexPath]insertSubview:subSelectionView aboveSubview:self.view];? - Aldee
如果您可以看到子视图,那么是的 - 问题在于它无法接收事件。 - Peter Sarnowski

6
[tableView bringSubviewToFront:subSelectionView];

如果不起作用,请尝试以下方法。
[viewController.view bringSubviewToFront:subSelectionView];

谢谢Moonkid,但还是不行。我仍然关注后视图。如果我使用insertViewAtIndex:方法,最上层是什么?是最大的数字/值还是最小的? - Aldee
您可以使用window对象获取最高层视图,代码如下:Window *window = [[[UIApplication sharedApplication]windows]objectAtIndex:0]; 然后,获取UIView类型的最顶层视图:UIView *topView = [[window subviews]lastObject]; - Moonkid

5
您可以将此子视图放在视图堆栈中更高的位置。这可以在Interface Builder中轻松完成。
这里,“Label - files”位于“Table View - all files”的上方。这不是您想要的吗?

4
使用UIView的- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index函数,结合从UIView的subViews属性获取的subviewsNSArray ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接