2014-09-01 35 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