iphone - 将 UIView 置于所有其他 View 之上

标签 iphone objective-c ios uiview uikit

有人可以帮助我如何使在代码中创建的 UIView 位于所有其他 View 之上吗?其中 tableView 是父 View ,subSelectionView 是我的自定义兄弟 View ,我想将它放在 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

最佳答案

UIView 实例有一个 bringSubViewToFront 方法。你可以使用那个方法。

例如

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

编辑

在你的情况下应该是,

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

关于iphone - 将 UIView 置于所有其他 View 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9075909/

相关文章:

objective-c - UITextView 顶部有奇怪的边距?

objective-c - 将静态库添加到 XCode 4 项目会破坏 "build and archive"功能

objective-c - 在Cocoa/Obj-C中,如何捕获cmd-c事件?

ios - 如何在 swift 中使用 async/await 同时运行多个 API 调用?

ios - 无法构建模块 UIKit

iphone - 无法设置文本标签单元格

iPhone CALayer 图像数组内容值

iphone - 在 dealloc 中使 NSTimer 无效的问题

ios - 将 Appcode 与 Kotlin/Native 和 Cocoapods 一起使用

ios - React native-iOS,“:CFBundleIdentifier”不存在