在子视图中居中UITableView

时间:2015-02-09 21:22:02

标签: ios objective-c uitableview cocoa-touch subview

如果单元格的总数*行高<1,我最终会尝试在UITableView中垂直居中我的表格视图单元格。 tableView的高度

我的问题是每当我尝试引用tableview高度时(应该在添加到superview时调整大小。它返回Interface Builder中定义的视图的原始高度。

以下是使用tableView添加subView的方法:

- (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]];
}
  • 子视图由UIView内部的tableView组成。在Interface Builder中,UIView的大小为320 X 586,tableView为310 x 558.
  • viewBottomContainer在IB中的大小为320 x 408

我试图在configureGUI方法中获取有关视图和tableView高度的信息。一旦有效,我将把它传递给另一个方法来在TableView中创建一个contentOffset。但NSLog不断返回原始值,而不是重新调整后的值。

这些是BSMissionListView类的方法:

- (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]];
}

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

1 个答案:

答案 0 :(得分:0)

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

-I将CGFloat变量添加到在BSMissionListView类中加载nib的方法

- 我传递了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;

}