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