简体   繁体   中英

Can't set a subview below another subview

This is the scenario:

在此处输入图片说明

I have this UIViewController of a MainVC class.

Inside this MainVC i have a UITextField of a TextFieldVC class.

Now inside TextFieldVC.m i'm creating a UITableView that should be inserted below the UITextField with this code

            //TextFieldVC.m

            UIView *rootView = [self.window.subviews objectAtIndex:0];

            [self.superview bringSubviewToFront:self];
            [rootView insertSubview:self.autoCompleteTableView
                             belowSubview:self];

where autoCompleteTableView is the UITableView . The UITableView will be created whenever the user starts editing the UITextField , that is an @Outlet in MainVC.m

Now the problem is that this autoCompleteTableView is created above the UITextField and i don't understand why.

A possible explanation for what is happening is that rootView does not have the text field among its subviews. Try this out:

        [self.superview insertSubview:self.autoCompleteTableView
                         belowSubview:self];

By doing this you are ensuring that you add the table view to the same view that also contains the text field, so the ordering between the two subviews can be enforced.

Strange behaviour, based on sergio answer.

If I have the root view frame at another location than 0,0 (exemple: self.view.frame= CGRectMake(0, 1100, 300, 400); ).

= when I call this line on the control, the root view return at 0,0 !!!

  [self.superview insertSubview:self.autoCompleteTableView
                         belowSubview:self];

Any idea?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM