简体   繁体   English

无法在另一个子视图下方设置子视图

[英]Can't set a subview below another subview

This is the scenario: 这是方案:

在此处输入图片说明

I have this UIViewController of a MainVC class. 我有一个MainVC类的UIViewController

Inside this MainVC i have a UITextField of a TextFieldVC class. 在这个MainVC中,我有一个TextFieldVC类的UITextField

Now inside TextFieldVC.m i'm creating a UITableView that should be inserted below the UITextField with this code 现在在TextFieldVC.m中,我正在创建一个UITableView ,应使用此代码将其插入 UITextField

            //TextFieldVC.m

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

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

where autoCompleteTableView is the UITableView . 其中autoCompleteTableViewUITableView The UITableView will be created whenever the user starts editing the UITextField , that is an @Outlet in MainVC.m 每当用户开始编辑UITextField (即@Outlet中的@Outlet) ,都会创建UITableView

Now the problem is that this autoCompleteTableView is created above the UITextField and i don't understand why. 现在的问题是,这个autoCompleteTableView是在UITextField上方创建的,我不明白为什么。

A possible explanation for what is happening is that rootView does not have the text field among its subviews. 关于发生的情况的可能解释是rootView的子视图中没有文本字段。 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. 基于sergio回答的奇怪行为。

If I have the root view frame at another location than 0,0 (exemple: self.view.frame= CGRectMake(0, 1100, 300, 400); ). 如果我的根视图框架位于除0,0以外的其他位置(例如: self.view.frame= CGRectMake(0, 1100, 300, 400); )。

= when I call this line on the control, the root view return at 0,0 !!! =当我在控件上调用此行时,根视图返回0,0 !!!

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

Any idea? 任何想法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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