2014-09-01 78 views
1

我的故事板中有一个桌面视图,其类设置为我的UITableView子类,名为SPSExplanationTableView。在Interface Builder中,这个tableview没有设置约束。使用自动布局在桌面视图顶部定位视图

我想以编程方式创建UIView显示在tableview中— which I know how to do(博客文章链接)的前—但其尺寸和使用自动布局定位。这是我的代码:

#import "SPSExplanationTableView.h" 

@interface SPSExplanationTableView() 

@property (nonatomic, strong) UIView *explanationView; 

@end 

@implementation SPSExplanationTableView 

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
    self.explanationView = [[UIView alloc] init]; 
    self.explanationView.translatesAutoresizingMaskIntoConstraints = NO; 
    self.explanationView.backgroundColor = [UIColor blueColor]; 

    [self addSubview:self.explanationView]; 
    [self bringSubviewToFront:self.explanationView]; 

NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.explanationView 
                    attribute:NSLayoutAttributeHeight 
                    relatedBy:NSLayoutRelationEqual 
                     toItem:nil 
                    attribute:NSLayoutAttributeNotAnAttribute 
                    multiplier:1.0f constant:150.0f]; 
[self.explanationView addConstraint:heightConstraint]; 

NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.explanationView 
                    attribute:NSLayoutAttributeWidth 
                    relatedBy:NSLayoutRelationEqual 
                     toItem:nil 
                    attribute:NSLayoutAttributeNotAnAttribute 
                    multiplier:1.0f constant:200.0f]; 
[self.explanationView addConstraint:widthConstraint]; 

NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.explanationView 
                   attribute:NSLayoutAttributeCenterY 
                   relatedBy:NSLayoutRelationEqual 
                    toItem:self 
                   attribute:NSLayoutAttributeCenterY 
                   multiplier:1.0f constant:0.0f]; 
[self addConstraint:topConstraint]; 

NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.explanationView 
                    attribute:NSLayoutAttributeCenterX 
                    relatedBy:NSLayoutRelationEqual 
                    toItem:self 
                    attribute:NSLayoutAttributeCenterX 
                   multiplier:1.0f constant:0.0f]; 
[self addConstraint:leftConstraint]; 

@end 

当我运行它具有以下断言失败崩溃的应用程序:

*** Assertion failure in -[SPSExplanationTableView layoutSublayersOfLayer:], 
/SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still 
required after executing -layoutSubviews. SPSExplanationTableView's 
implementation of -layoutSubviews needs to call super.'

以消息的字面和压倒一切的layoutSubviews没有效果即我仍然得到同样的崩溃。

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
} 

什么是实现我想要实现的正确方法?


对于Aubada Taljo,这里是在Interface Builder中实现代码如下:

enter image description here


更新:我到底解决了这个自己不使用自动布局!我重写了我的SPSExplanationTableView类中的layoutSubviews方法,并将explainView的中心属性设置为自身边界的中心,稍微调整y轴位置以使其看起来如何。

回答

2

这是崩溃,因为UITableViews不是为了这样做而设计的。一个UITableView只关心它的单元格,头文件,也许它的背景,并且它没有使用自动布局的逻辑。因此,如果您尝试将其纳入其与任何子视图之间的任何约束计算中,它都会崩溃,例如,如果您在单元格和表格之间添加约束,这也会崩溃。

我建议你做的是增加一个上海华将同时包含您的表和要叠加的观点:

SuperviewWithConstraints 
    | 
    |-- YourTableViewWithConstraintsRelativeToSuperview 
    | 
    |-- YourOverlayWithConstraintsRelativeToSuperview 

,并成立了制约那里。然后将该超级视图作为视图控制器的视图。不过,您将不得不使用UITableViewController作为控制类。

0

如果您愿意接受我的建议并让您的生活更轻松,只需转到Interface Builder中的故事板,并在您的表格视图中设置正确的约束,现在回到代码编辑器以及拥有您的表格的UIViewController查看,在viewDidLoad中写入视图创建代码

UIView* someView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height)]; 
[self.view addSubView someView]; 

我想这应该是绰绰有余解决您的问题,现在如果你遇到任何问题,请将您的代码viewDidLayoutSubViews在UIViewController中

请告诉我,如果你需要更多的细节,但我用这种始终创建动态控件的方式。

+0

谢谢,但我不知道如何在Interface Builder中的tableview上设置正确的约束,因为子视图在那里不存在。另外,我并不特别想用视图创建代码来混乱我的视图控制器。这感觉就像我应该能够使用一个自包含的tableview类。 – 2014-09-02 07:25:06

+0

关键是您不必为子视图(someView)设置正确的约束,您只需在Interface Builder中为表视图设置正确的约束,上面的代码将完成剩下的工作。 此外,它不是很好的添加视图作为UITableView的一个孩子,因为它不是为此设计的,所以上面的代码会告诉你你想要什么 – 2014-09-02 09:34:40

+0

那么tableview的正确约束是什么? – 2014-09-02 09:46:37