[英]Place UIView on top of all other views
有人可以帮助我如何使代码中的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。 有人能帮助我吗?....
UIView实例有一个方法bringSubViewToFront。 您可以使用该方法。
例如
UIVIew *yourSubView;
[yourMainView addSubView:yourSubView];
[yourMainView bringSubviewToFront:yourSubView];
编辑
你的情况应该是这样的,
[[tableView cellForRowAtIndexPath:indexPath] insertSubview:subSelectionView aboveSubview:self.view];
[[tableView cellForRowAtIndexPath:indexPath] bringSubviewToFront:subSelectionView];
如果我正确地阅读了你的问题,你的问题是你实际上无法点击新视图中位于桌面视图之上的按钮?
当你点击它时,下面的UITableView会响应?
这意味着UIButton无法识别它被点击,并且事件将转到它下面的下一个子视图进行处理。
现在,这种行为有很多可能的原因 - 你可能已禁用用户交互或UIView ,它可能是Enabled属性设置为NO或者它可能超出了UIView的绑定 (iOS自动假设如果点击是在界限矩形之外,它错过了接收器)。
此外,如果您的alpha设置为0.0,则会忽略点击。
你应该检查以上所有内容。
在Swift中你可以这样做:
self.view.bringSubviewToFront(theViewToMoveToFront)
[tableView bringSubviewToFront:subSelectionView];
或者如果不工作试试
[viewController.view bringSubviewToFront:subSelectionView];
您可以将此子视图放在视图堆栈中的较高位置。 这可以在Interface Builder中轻松完成。
这里Label - files
放在Table View - all files
顶部Table View - all files
。 这不是你想要的吗?
使用- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
UIView
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
函数以及从UIView的subViews属性获得的子视图的NSArray ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.