【问题标题】:Place UIView on top of all other views将 UIView 放在所有其他视图之上
【发布时间】:2012-02-22 22:59:04
【问题描述】:

有人可以帮助我如何使在代码中创建的 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。有人可以帮我吗?....

【问题讨论】:

    标签: iphone objective-c ios uiview uikit


    【解决方案1】:

    在 Swift 中你可以这样做:

    self.view.bringSubviewToFront(theViewToMoveToFront)
    

    【讨论】:

    • 在 Swift 4.2 中现在是:view.bringSubviewToFront(theViewToMoveToFront)
    【解决方案2】:

    UIView 实例有一个方法bringSubViewToFront。您可以使用该方法。

    例如

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

    编辑

    在你的情况下应该是这样的,

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

    【讨论】:

    • 能否请您发布您的完整源代码如何创建子视图和添加?
    • @AldeeMativo: bringSubviewToFront: 仅适用于直接子视图,不适用于任何后代视图。您需要将消息发送到表格视图单元格,而不是视图控制器的视图。
    【解决方案3】:

    如果我没看错你的问题,你的问题是你实际上不能点击表格视图顶部的新视图中的按钮?

    当你点击它时,下面的 UITableView 会响应吗?

    这意味着 UIButton 无法识别它被点击,并且事件转到它下面的下一个子视图进行处理。

    现在,这种行为有很多可能的原因 - 您可能为它或 UIView 禁用了用户交互,它可能是 Enabled 属性设置为 NO 或它可能超出 UIView 的约束(iOS 自动假设如果点击超出边界矩形,它会错过接收器)。

    此外,如果您的 alpha 设置为 0.0,则会忽略点击。

    您应该检查以上所有内容。

    【讨论】:

    • 谢谢皮特..我已经查看了它并将按钮启用属性设置为是但仍然不起作用..嗯..我使用此代码是否正确:[[tableView cellForRowAtIndexPath:indexPath]insertSubview :subSelectionView aboveSubview:self.view];?
    • 如果你可以看到子视图,那么是的——问题是它没有接收到事件。
    【解决方案4】:
    [tableView bringSubviewToFront:subSelectionView];
    

    或者如果不工作尝试

    [viewController.view bringSubviewToFront:subSelectionView];
    

    【讨论】:

    • 感谢moonkid,仍然无法正常工作。我仍然专注于后视图.. 嗯.. 如果我将使用 insertViewAtIndex: 方法,最顶层是什么?它是最大的数字/值还是最小的?
    • 您可以使用窗口对象 Window *window = [[[UIApplication sharedApplication]windows]objectAtIndex:0];比 UIView *topView = [[窗口子视图]lastObject];
    【解决方案5】:

    使用 UIView- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index 函数以及从 UIView 的 subViews 属性获得的 subviewsNSArray ...

    【讨论】:

      【解决方案6】:

      您可以将此子视图放在视图堆栈中的较高位置。这可以在 Interface Builder 中轻松完成。

      这里Label - files 位于Table View - all files 之上。这不是你想要的吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 2012-12-24
        • 1970-01-01
        • 1970-01-01
        • 2018-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多