16

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:

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

- (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?....

enter image description here

0

6 Answers 6

53

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

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];
2
  • Can you please post your full source code how do you create subview and add ? Jan 31, 2012 at 7:50
  • @AldeeMativo: bringSubviewToFront: only works with a direct subview, not any descendant view. You need to send the message to the table view cell, not the view controller's view. Jan 31, 2012 at 9:09
8

In Swift you can do this:

self.view.bringSubviewToFront(theViewToMoveToFront)
1
  • 1
    In Swift 4.2 it is now: view.bringSubviewToFront(theViewToMoveToFront) Feb 20, 2019 at 16:40
7

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?

And when you tap it, the UITableView below responds?

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

Now, there are many possible causes for such behavior - you may have user interaction disabled for it or the UIView, it may it's Enabled property 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).

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

You should check all of the above.

2
  • Thanks pete.. I had already reviewed it and set the button enabled property to yes but still does not work.. hmmm.. am I right using this code: [[tableView cellForRowAtIndexPath:indexPath]insertSubview:subSelectionView aboveSubview:self.view];?
    – Aldee
    Jan 31, 2012 at 8:16
  • If you can see the subview, then yes - the problem is that it does not receive events. Jan 31, 2012 at 8:38
6
[tableView bringSubviewToFront:subSelectionView];

or if don`t work try

[viewController.view bringSubviewToFront:subSelectionView];
2
  • thanks moonkid, still does not work.. I am still on focus with the back view.. hmm.. if I will use insertViewAtIndex: method, what is the topmost layer? is it the greatest number / value or the lowest?
    – Aldee
    Jan 31, 2012 at 7:41
  • You can get topest view using window object Window *window = [[[UIApplication sharedApplication]windows]objectAtIndex:0]; Than UIView *topView = [[window subviews]lastObject];
    – Moonkid
    Jan 31, 2012 at 7:55
5

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

enter image description here

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

0
4

Use - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index function of UIView along with the NSArray of subviews which you get from UIView's subViews property ...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.