2012-01-31 33 views
13

有人可以幫助我如何使在代碼中創建的UIView位於所有其他視圖之上?其中tableView是父視圖和subSelectionView是我的自定義兄弟姐妹視圖,我想使它在tableView頂部..我有下面的代碼:在所有其他視圖上放置UIView

這是我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

回答

6

如果我讀了你的問題的權利你的問題是,你不能真正在新視圖按鈕這是對的tableview的頂部挖掘?

當你點擊它,下面的UITableView響應?

這意味着UIButton不會識別它被輕敲,並且事件會轉到它下面要處理的下一個子視圖。

現在,有這種行爲的許多可能的原因 - 你可能有它的用戶交互禁用或的UIView,可能它的啓用屬性設置爲NO,或者它可能超出的UIView的債券(iOS會自動假定如果水龍頭超出邊界矩形,它會錯過接收器)。

此外,如果您的alpha設置爲0.0,則會忽略輕敲。

您應該檢查以上所有內容。

+0

感謝皮特..我已經審查了它,並將按鈕啓用屬性設置爲是,但仍然無法正常工作..嗯..我正確使用此代碼:[[tableView cellForRowAtIndexPath:indexPath] insertSubview:subSelectionView aboveSubview:self.view ];? – Aldee 2012-01-31 08:16:19

+0

如果你能看到子視圖,那麼是的 - 問題是它沒有收到事件。 – 2012-01-31 08:38:52

5

你可以放在視圖棧這個子視圖高。這可以在Interface Builder中輕鬆完成。

enter image description here

這裏Label - files放置ontop的的Table View - all files。這不是你想要的嗎?

+0

感謝srikar,而是由代碼生成標籤不IB ... – Aldee 2012-01-31 07:39:55

47

有一個方法bringSubViewToFront的UIView實例。您可以使用該方法。

例如

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

編輯

在你的情況應該是這樣

UIViewNSArray的子視圖你從UIView的的子視圖相處

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

感謝jennis,但仍然沒有工作對我來說... – Aldee 2012-01-31 07:40:09

+0

你能請發佈您的完整源代碼如何創建子視圖並添加? – 2012-01-31 07:50:43

+0

請參閱最後一部分..謝謝.. – Aldee 2012-01-31 07:54:54

4

使用- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index功能財產...

+0

我可以有一些Jhaliya樣本嗎?謝謝.. – Aldee 2012-01-31 07:41:51

5
[tableView bringSubviewToFront:subSelectionView]; 

,或者如果不`噸的工作嘗試

[viewController.view bringSubviewToFront:subSelectionView]; 
+0

感謝moonkid,仍然無法正常工作..我仍然關注後視圖..嗯..如果我將使用insertViewAtIndex:方法,什麼是最頂層?這是最大的數字/價值還是最低的? – Aldee 2012-01-31 07:41:29

+0

您可以使用窗口對象Window * window = [[[UIApplication sharedApplication]窗口] objectAtIndex:0]獲得最佳視圖;比UIView * topView = [[window subviews] lastObject]; – Moonkid 2012-01-31 07:55:26

0

在斯威夫特,你可以這樣做:

self.view.bringSubview(toFront: theViewToMoveToFront) 
相關問題