1

加入現有的自動版式視圖我有一個像下面這有自動佈局xibs設置視圖。 enter image description here添加自動佈局編程到xibs

現在我需要添加一個錯誤通知,將從頂部有生命的和現有的意見將下移,像下面這個圖片。

enter image description here

我知道我可以通過簡單地在我現有的視圖控制器將這個錯誤觀點和管理其高度的限制很容易地做到這一點。但我有一些其他視圖,我需要重新使用這個錯誤視圖。所以,現在我爲這個錯誤視圖創建了一個自定義視圖。現在我的主要問題是通過自動佈局以編程方式將此添加到我的主視圖。所以我需要在我的self.view中添加藍色錯誤視圖,並移除綠色視圖的頂部佈局約束,並將其頂部設置爲藍色錯誤視圖。合理?以下是我的錯誤視圖代碼並將其添加到self.view。但即使這樣也行不通,我在這裏做錯了什麼。任何幫助表示讚賞。

-(id)initWithdelegate:(id)parentSelf ForView:(UIView *)parentView 
{ 
    if (self = [super initWithFrame:CGRectMake(0, 0, 100, 100)]) 
    { 

     // Initialization code 
     self=(ErrorView*)[[[NSBundle mainBundle] loadNibNamed:@"ErrorView" owner:nil options:nil]objectAtIndex:0]; 

     self.delegate=parentSelf; 

     [parentView addSubview:self]; 
     self.hidden = YES; 
     [self setConstraintsToParentView:parentView]; 

    } 

    return self; 
} 

-(void)setConstraintsToParentView:(UIView *)parentView 
{ 

    [parentView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    //Setting width equal to parentview 
    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:self 
                  attribute:NSLayoutAttributeWidth 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:parentView 
                  attribute:NSLayoutAttributeWidth 
                 multiplier:1 
                  constant:0]]; 

    //Setting fixed height of 50 
    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:self 
                 attribute:NSLayoutAttributeHeight 
                 relatedBy:NSLayoutRelationGreaterThanOrEqual 
                  toItem:nil 
                 attribute:NSLayoutAttributeNotAnAttribute 
                 multiplier:1.0 
                  constant:50]]; 

    //Setting x pos to center 
    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:self 
                  attribute:NSLayoutAttributeCenterX 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:parentView 
                  attribute:NSLayoutAttributeCenterX 
                 multiplier:1.0 
                  constant:0.0]]; 

    //Setting top position to self.view with constant 20 
    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:self 
                  attribute:NSLayoutAttributeTop 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:parentView 
                  attribute:NSLayoutAttributeTop 
                 multiplier:1.0 
                  constant:20]]; 

} 

我從我的視圖 - 控制

myErrorView = [[ErrorView alloc]initWithdelegate:self ForView:self.view]; 
+0

究竟它是如何「不工作」? – Kreiri

+0

目前它給我錯誤,無法同時滿足約束。 – Gamerlegend

+0

哪些限制? – Kreiri

回答

1

你可以使用約束

NSArray *ArrayConstraint = yorlableorView.constraints; 
for (NSLayoutConstraint *ob in ArrayConstraint) 
     { 

        if ([ob.identifier isEqualToString:@"yourIdentifier"]) 
        { 
         ob.constant = 0 ; 
       // set your constant you want 
        } 
      } 
1

的標識propertiy只需要添加錯誤觀點的頂視圖調用這樣的errorView高度爲0,然後在想要顯示高度約束時更改高度約束?您可以對此進行動畫處理,並將模擬將所有內容向下推動,但採用這種方法時,它不會看起來像從屏幕的頂部下來。

如果您對此方法感到滿意,您可以使用按鈕和tableview將容器視圖添加到視圖中,然後在從xib初始化錯誤視圖後將其分配給容器,這將爲您節省時間傳遞父視圖,將其添加爲子視圖並以編程方式在錯誤視圖類中添加約束。

讓我知道,如果需要更多的細節或具體的例子。希望這有幫助,祝你好運。另外,您可以查看Masonry,它使編程更容易添加約束。