美文网首页
masonry设置约束后如何获取正确的frame

masonry设置约束后如何获取正确的frame

作者: Mickey__chen | 来源:发表于2017-11-06 23:22 被阅读0次

纯代码实现自定义布局,我通常使用masonry框架和设置frame同时进行,然而masonry设置约束后并不能直接反应到frame上.

约束转frame需要时间,设置约束后直接使用frame结果全部为(0,0,0,0).比如:

- (void)setTableView {

UITableView *tableView = [[UITableView alloc] init];

tableView.frame = CGRectMake(0, CGRectGetMaxY(self.midView.frame), WSWIDTH, WSHEIGHT - CGRectGetMaxY(self.midView.frame) - 150 - 49 );

tableView.tableFooterView = [[UIView alloc] init];

tableView.delegate = self;

tableView.dataSource = self;

self.tableView = tableView;

[tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WSSizeNumCell class]) bundle:nil] forCellReuseIdentifier:joinGoodsTrainsId];

[self.contentV addSubview:tableView];

}

这里想利用之前用masonry约束好的midView的frame设置tableView的位置,但是这里self.midView.frame仍然为(0,0,0,0)

要想获取到正确的frame,要利用到layoutIfNeeded

layoutIfNeeded:告知页面需要立即更新,如果希望立即生成新的frame需要调用此方法.

如下:

- (void)setTableView {

[self.view layoutIfNeeded];

UITableView *tableView = [[UITableView alloc] init];

tableView.frame = CGRectMake(0, CGRectGetMaxY(self.midView.frame), WSWIDTH, WSHEIGHT - CGRectGetMaxY(self.midView.frame) - 150 - 49 );

tableView.tableFooterView = [[UIView alloc] init];

tableView.delegate = self;

tableView.dataSource = self;

self.tableView = tableView;

[tableView registerNib:[UINib nibWithNibName:NSStringFromClass([WSSizeNumCell class]) bundle:nil] forCellReuseIdentifier:joinGoodsTrainsId];

[self.contentV addSubview:tableView];

}

这个时候获取frame就是正确的.

相关文章

网友评论

      本文标题:masonry设置约束后如何获取正确的frame

      本文链接:https://www.haomeiwen.com/subject/jcgrmxtx.html