ios 将 UIView 放在所有其他视图之上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9075909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:35:11  来源:igfitidea点击:

Place UIView on top of all other views

iphoneobjective-ciosuiviewuikit

提问by Aldee

Can someone help me on how to make make UIView created in code to be on top of all other view? where tableView is the parent view and the subSelectionView is my custom siblings view, I want to make it on top of the tableView.. I have this code below:

有人可以帮助我如何使在代码中创建的 UIView 位于所有其他视图之上吗?其中 tableView 是父视图,subSelectionView 是我的自定义兄弟视图,我想把它放在 tableView 之上。我有下面的代码:

This is the full source code for my didSelectRowAtIndexPath:, from where I programmatically add UIView instance..

这是我的 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];

}

but does not work..

但不起作用..

The image below shows my subSelectionView, the button is within it and the subSelectionView is within the tableView. Now, when I clicked that button, the problem is that I can't actually click it. It focuses directly to the tableView even with the presence of my codes. Can someone help me please?....

下图显示了我的 subSelectionView,按钮位于其中,而 subSelectionView 位于 tableView 中。现在,当我单击该按钮时,问题是我实际上无法单击它。即使存在我的代码,它也直接关注 tableView。有人能帮助我吗?....

enter image description here

在此处输入图片说明

采纳答案by Peter Sarnowski

If I read your question right your problem is that you can't actually tap on the button in your new view that's on top of the tableview?

如果我读对了您的问题,您的问题是您实际上无法点击位于 tableview 顶部的新视图中的按钮?

And when you tap it, the UITableView below responds?

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

This means that the UIButton does not recognize it is tapped, and the event goes to the next subview below it to be processed.

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

Now, there are many possible causes for such behavior - you may have user interaction disabled for it or the UIView, it may it's Enabledproperty set to NO or it may be beyond the bonds of the UIView(iOS automatically assumes that if a tap is outside of the bounds rectangle it missed the receiver).

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

Also, taps are ignored if your alpha is set to 0.0.

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

You should check all of the above.

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

回答by Janak Nirmal

There is a method bringSubViewToFront of UIView instance. You can use that method.

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

e.g.

例如

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

EDIT

编辑

in your case it should be like,

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

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

回答by pableiros

In Swift you can do this:

在 Swift 中,你可以这样做:

self.view.bringSubviewToFront(theViewToMoveToFront)

回答by Moonkid

[tableView bringSubviewToFront:subSelectionView];

or if don`t work try

或者如果不工作尝试

[viewController.view bringSubviewToFront:subSelectionView];

回答by Srikar Appalaraju

You could place this subview higher in the view stack. This can be easily done in Interface Builder.

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

enter image description here

在此处输入图片说明

Here Label - filesis placed ontop of Table View - all files. Isn't that what you want?

这里Label - files放置在Table View - all files. 这不是你想要的吗?

回答by Jhaliya

Use - (void)insertSubview:(UIView *)view atIndex:(NSInteger)indexfunction of UIViewalong with the NSArrayof subviewswhich you get from UIView's subViewsproperty ...

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