在子视图中居中 UITableView

Center UITableView within a subView

如果单元格总数 * 行高 < tableView

的高度,我最终会尝试将 UITableView 中的 table 视图单元格垂直居中

我的问题是,每当我尝试引用 table 视图高度(添加到它的超级视图时应该调整大小。它 returns Interface Builder 中定义的视图的原始高度。

下面是添加子视图和 tableView 的方法:

- (void)addMissonListView {
    missionView = [BSMissionListView loadMissionView:YES];
    BSList *list = (BSList *)self.dataModel;
    missionView.missionList = list.arrayMissions;
    missionView.delegate = self;
    missionView.heightofSuperView = [NSNumber numberWithFloat:viewBottomContainer.frame.size.height];
    [viewBottomContainer addSubview:missionView];


    [viewBottomContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
    [BSUtility setConstraintsOnView:viewBottomContainer relativeTo:missionView withConstant:0];   
}

这是调用的 setsConstraintsOnView 方法:

+ (void)setConstraintsOnView:(UIView *)superView relativeTo:(UIView *)subView withConstant:(int)constant
{
    [subView setTranslatesAutoresizingMaskIntoConstraints:NO];

    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1 constant:constant]];

    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1 constant:constant]];

    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight multiplier:1 constant:constant]];

    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft multiplier:1 constant:constant]];
}

我试图在 configureGUI 方法中获取有关视图和 table视图高度的信息。一旦成功,我会将其传递给另一种方法以在 TableView 中创建 contentOffset。但是 NSLog 一直返回原始值,而不是调整后的值。

这些是来自 BSMissionListView 的方法class:

- (void)awakeFromNib {
    [super awakeFromNib];

    [self configureGUI];
}

- (void)configureGUI {
    [super configureGUI];

    NSLog(@"Bounds height of the View is %f",self.bounds.size.height);
    NSLog(@"Bounds height of the tableView is %f",self.tableViewMissionList.bounds.size.height);
    [self setBackgroundColor:[UIColor clearColor]];
}

如果有帮助,我很乐意 post 更多代码。谢谢!

我通过加载tableView nib的方法传入tableView的superView的高度来解决问题。

-我在 BSMissionListView 中加载 nib 的方法中添加了一个 CGFloat 变量 Class

-我传入了viewBottomContainer的高度,然后在新修改的方法中用它来设置heightOfSuperView的值。

-heightOfSuperView 的值用于计算 TableView 的 contentInset 以使其在 superView 中居中

感谢@Alex Renyolds 帮助我。

- (void)addMissonListView {
[viewBottomContainer setNeedsLayout];
[viewBottomContainer layoutIfNeeded];
[viewBottomContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLog(@"Height of bottomViewContrainer: %f", viewBottomContainer.bounds.size.height);
BSList *list = (BSList *)self.dataModel;
missionView = [BSMissionListView loadMissionView:YES heightOfSuperView:viewBottomContainer.bounds.size.height listArray:list.arrayMissions];

missionView.delegate = self;
[viewBottomContainer addSubview:missionView];
[viewBottomContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
[BSUtility setConstraintsOnView:viewBottomContainer relativeTo:missionView withConstant:0];

}

BSMissionList

+ (BSMissionListView *)loadMissionView:(BOOL)isEditable heightOfSuperView:(CGFloat)height listArray:(NSMutableArray *)list {
BSMissionListView *missionListView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([BSMissionListView class]) owner:nil options:nil] lastObject];
missionListView.missionList = list;
missionListView.isEditable = isEditable;
missionListView.heightofSuperView = (height- 49);
[missionListView configureGUI];

return missionListView;

}