简体   繁体   English

窗口外的表视图屏幕iOS目标C

[英]Table View out of the window screen iOS objective C

I have set constraint for my text field in story board. 我为故事板上的文本字段设置了约束。 I want to create a table view under the text field with the following code. 我想使用以下代码在文本字段下创建一个表格视图。 Setting the width similar to the textfield.frame.size.width brings the tableView all the way out of the screen, why? 设置类似于textfield.frame.size.width的宽度会将tableView完全移出屏幕,为什么? Please help. 请帮忙。

-(void)cofigureAutoComTableView
{

    autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.txtActivity.frame.origin.x,self.txtActivity.frame.origin.y+32,self.txtActivity.frame.size.width, 200) style:UITableViewStylePlain];

    autocompleteTableView.delegate = self;
    autocompleteTableView.dataSource = self;
    autocompleteTableView.scrollEnabled = YES;
    //autocompleteTableView.
    autocompleteTableView.hidden = YES;
    [self.view addSubview:autocompleteTableView];

    CALayer *layer = autocompleteTableView.layer;
    [layer setMasksToBounds:YES];
    [layer setCornerRadius: 4.0];
    [layer setBorderWidth:1.0];
    [layer setBorderColor:[[UIColor blackColor] CGColor]];
}

图片

看起来很像,因为当textview这么长时您正在调用函数,因此请在viewDidAppear使用它

[self cofigureAutoComTableView];

You can try 你可以试试

 -(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    if(once)
    {
        once = NO;

        [self conf];

   }

}

The problem is that in viewDidLoad the frames are not in the final form. 问题在于,在viewDidLoad中,框架不是最终形式。 Only in viewDidAppear the frames are final. 只有在viewDidAppear中,框架才是最终的。

The best solution in this case is to update the autocompleteTableView's frame in viewDidLayoutSubviews. 在这种情况下,最好的解决方案是在viewDidLayoutSubviews中更新autocompleteTableView的框架。

viewDidLayoutSubviews is called every time the viewController's view frame is updated. 每当更新viewController的视图框架时,都会调用viewDidLayoutSubviews。

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    autocompleteTableView.frame = CGRectMake(self.txtActivity.frame.origin.x,self.txtActivity.frame.origin.y+32,self.txtActivity.frame.size.width, 200);

}

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

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