简体   繁体   中英

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? 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. Only in viewDidAppear the frames are final.

The best solution in this case is to update the autocompleteTableView's frame in viewDidLayoutSubviews.

viewDidLayoutSubviews is called every time the viewController's view frame is updated.

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

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

}

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