Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place UIView on top of all other views

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

like image 762
Aldee Avatar asked Jan 31 '12 07:01

Aldee


4 Answers

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];
like image 52
Janak Nirmal Avatar answered Nov 04 '22 00:11

Janak Nirmal


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.

like image 34
Peter Sarnowski Avatar answered Nov 04 '22 01:11

Peter Sarnowski


[tableView bringSubviewToFront:subSelectionView];

or if don`t work try

[viewController.view bringSubviewToFront:subSelectionView];
like image 6
Moonkid Avatar answered Nov 04 '22 00:11

Moonkid


In Swift you can do this:

self.view.bringSubviewToFront(theViewToMoveToFront)
like image 6
pableiros Avatar answered Nov 03 '22 23:11

pableiros